Chinaunix首页 | 论坛 | 博客
  • 博客访问: 165169
  • 博文数量: 56
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 593
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-18 09:59
文章分类

全部博文(56)

文章存档

2019年(1)

2018年(26)

2016年(1)

2015年(6)

2014年(22)

我的朋友

分类: Java

2014-09-14 22:48:49


使用方法:
        OSinfo.isWindows()
        OSinfo.isLinux()


添加相应jar包到Java项目即可,  jar包:(保留作者信息)
      jar下载位置:


点击(此处)折叠或打开

  1. /**
  2.  * @author:      吴永行
  3.  * @dateTime:     2014-8-20 下午3:55:43
  4.  * @description:     
  5.  *
  6.  */

  7. public enum EPlatform {
  8.          Any("any"),
  9.          Linux("Linux"),
  10.          Mac_OS("Mac OS"),
  11.          Mac_OS_X("Mac OS X"),
  12.          Windows("Windows"),
  13.          OS2("OS/2"),
  14.          Solaris("Solaris"),
  15.          SunOS("SunOS"),
  16.          MPEiX("MPE/iX"),
  17.          HP_UX("HP-UX"),
  18.          AIX("AIX"),
  19.          OS390("OS/390"),
  20.          FreeBSD("FreeBSD"),
  21.          Irix("Irix"),
  22.          Digital_Unix("Digital Unix"),
  23.          NetWare_411("NetWare"),
  24.          OSF1("OSF1"),
  25.          OpenVMS("OpenVMS"),
  26.          Others("Others");
  27.       
  28.     private EPlatform(String desc){
  29.         this.description = desc;
  30.     }
  31.       
  32.     public String toString(){
  33.         return description;
  34.     }
  35.       
  36.     private String description;
  37. }


点击(此处)折叠或打开

  1. /**
  2.  * @author isea533
  3.  * @author:      吴永行
  4.  * @dateTime:     2014-8-20 下午3:54:07
  5.  * @description:     
  6.  *
  7.  */
  8. package com.myhexin.tool;

  9. public class OSinfo {
  10.     
  11.     private static String OS = System.getProperty("os.name").toLowerCase();
  12.       
  13.     private static OSinfo _instance = new OSinfo();
  14.       
  15.     private EPlatform platform;
  16.       
  17.     private OSinfo(){}
  18.       
  19.     public static boolean isLinux(){
  20.         return OS.indexOf("linux")>=0;
  21.     }
  22.       
  23.     public static boolean isMacOS(){
  24.         return OS.indexOf("mac")>=0&&OS.indexOf("os")>0&&OS.indexOf("x")<0;
  25.     }
  26.       
  27.     public static boolean isMacOSX(){
  28.         return OS.indexOf("mac")>=0&&OS.indexOf("os")>0&&OS.indexOf("x")>0;
  29.     }
  30.       
  31.     public static boolean isWindows(){
  32.         return OS.indexOf("windows")>=0;
  33.     }
  34.       
  35.     public static boolean isOS2(){
  36.         return OS.indexOf("os/2")>=0;
  37.     }
  38.       
  39.     public static boolean isSolaris(){
  40.         return OS.indexOf("solaris")>=0;
  41.     }
  42.       
  43.     public static boolean isSunOS(){
  44.         return OS.indexOf("sunos")>=0;
  45.     }
  46.       
  47.     public static boolean isMPEiX(){
  48.         return OS.indexOf("mpe/ix")>=0;
  49.     }
  50.       
  51.     public static boolean isHPUX(){
  52.         return OS.indexOf("hp-ux")>=0;
  53.     }
  54.       
  55.     public static boolean isAix(){
  56.         return OS.indexOf("aix")>=0;
  57.     }
  58.       
  59.     public static boolean isOS390(){
  60.         return OS.indexOf("os/390")>=0;
  61.     }
  62.       
  63.     public static boolean isFreeBSD(){
  64.         return OS.indexOf("freebsd")>=0;
  65.     }
  66.       
  67.     public static boolean isIrix(){
  68.         return OS.indexOf("irix")>=0;
  69.     }
  70.       
  71.     public static boolean isDigitalUnix(){
  72.         return OS.indexOf("digital")>=0&&OS.indexOf("unix")>0;
  73.     }
  74.       
  75.     public static boolean isNetWare(){
  76.         return OS.indexOf("netware")>=0;
  77.     }
  78.       
  79.     public static boolean isOSF1(){
  80.         return OS.indexOf("osf1")>=0;
  81.     }
  82.       
  83.     public static boolean isOpenVMS(){
  84.         return OS.indexOf("openvms")>=0;
  85.     }
  86.       
  87.     /**
  88.      * 获取操作系统名字
  89.      * @return 操作系统名
  90.      */
  91.     public static EPlatform getOSname(){
  92.         if(isAix()){
  93.             _instance.platform = EPlatform.AIX;
  94.         }else if (isDigitalUnix()) {
  95.             _instance.platform = EPlatform.Digital_Unix;
  96.         }else if (isFreeBSD()) {
  97.             _instance.platform = EPlatform.FreeBSD;
  98.         }else if (isHPUX()) {
  99.             _instance.platform = EPlatform.HP_UX;
  100.         }else if (isIrix()) {
  101.             _instance.platform = EPlatform.Irix;
  102.         }else if (isLinux()) {
  103.             _instance.platform = EPlatform.Linux;
  104.         }else if (isMacOS()) {
  105.             _instance.platform = EPlatform.Mac_OS;
  106.         }else if (isMacOSX()) {
  107.             _instance.platform = EPlatform.Mac_OS_X;
  108.         }else if (isMPEiX()) {
  109.             _instance.platform = EPlatform.MPEiX;
  110.         }else if (isNetWare()) {
  111.             _instance.platform = EPlatform.NetWare_411;
  112.         }else if (isOpenVMS()) {
  113.             _instance.platform = EPlatform.OpenVMS;
  114.         }else if (isOS2()) {
  115.             _instance.platform = EPlatform.OS2;
  116.         }else if (isOS390()) {
  117.             _instance.platform = EPlatform.OS390;
  118.         }else if (isOSF1()) {
  119.             _instance.platform = EPlatform.OSF1;
  120.         }else if (isSolaris()) {
  121.             _instance.platform = EPlatform.Solaris;
  122.         }else if (isSunOS()) {
  123.             _instance.platform = EPlatform.SunOS;
  124.         }else if (isWindows()) {
  125.             _instance.platform = EPlatform.Windows;
  126.         }else{
  127.             _instance.platform = EPlatform.Others;
  128.         }
  129.         return _instance.platform;
  130.     }
  131.     /**
  132.      * @param args
  133.      */
  134.     public static void main(String[] args) {
  135.         System.out.println(OSinfo.getOSname());
  136.     }
  137.   
  138. }




阅读(2994) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~