Java 判断操作系统类型(适用于各种操作系统)

来源:互联网 发布:淘宝搜索优化软件 编辑:程序博客网 时间:2024/05/16 14:55

最近一段时间写一个授权的程序,需要获取很多信息来保证程序不能随意复制使用,必须经过授权才可以。


为了限制用户使用的操作系统,必须有统一的方法来获取才可以。


在Java中,通过System.getProperty("os.name")来获取,通过参考:http://lopica.sourceforge.net/os.html 来实现各操作系统的判断。


针对windows系统,这里不具体判断是那个版本,如果有需要,可以在判断出windows之后,继续判断,判断需要考虑java的版本,版本不同,结果也不一样。


下面上代码:

1.枚举类型:EPlatform

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

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

我使用的Windows 7 识别出来:Windows ,如果大家使用别的操作系统,希望能把操作系统和结果在这里留言写下来。


如果结果错误,你可以使用下面的代码获取你的操作系统信息:

[java] view plain copy
  1. class WhatOS   
  2. {  
  3.   public static void main( String args[] )   
  4.   {  
  5.     System.out.println( System.getProperty("os.name") );  
  6.     System.out.println( System.getProperty("os.version") );  
  7.     System.out.println( System.getProperty("os.arch") );  
  8.   }  

0 0