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

简介: 最近一段时间写一个授权的程序,需要获取很多信息来保证程序不能随意复制使用,必须经过授权才可以。为了限制用户使用的操作系统,必须有统一的方法来获取才可以。在JAVA中,通过System.getProperty("os.name")来获取,通过参考:http://lopica.sourceforge.net/os.html 来实现各操作系统的判断。针对windows系统,这里不具体判断

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


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


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


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


下面上代码:

1.枚举类型:EPlatform

  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. }  
/**
 * 平台
 * @author isea533
 */
public enum EPlatform {
	Any("any"),
	Linux("Linux"),
	Mac_OS("Mac OS"),
	Mac_OS_X("Mac OS X"),
	Windows("Windows"),
	OS2("OS/2"),
	Solaris("Solaris"),
	SunOS("SunOS"),
	MPEiX("MPE/iX"),
	HP_UX("HP-UX"),
	AIX("AIX"),
	OS390("OS/390"),
	FreeBSD("FreeBSD"),
	Irix("Irix"),
	Digital_Unix("Digital Unix"),
	NetWare_411("NetWare"),
	OSF1("OSF1"),
	OpenVMS("OpenVMS"),
	Others("Others");
	
	private EPlatform(String desc){
		this.description = desc;
	}
	
	public String toString(){
		return description;
	}
	
	private String description;
}

2.操作系统类:OSinfo

  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. }  
/**
 * 操作系统类:
 * 获取System.getProperty("os.name")对应的操作系统
 * @author isea533
 */
public class OSinfo {
	
	private static String OS = System.getProperty("os.name").toLowerCase();
	
	private static OSinfo _instance = new OSinfo();
	
	private EPlatform platform;
	
	private OSinfo(){}
	
	public static boolean isLinux(){
		return OS.indexOf("linux")>=0;
	}
	
	public static boolean isMacOS(){
		return OS.indexOf("mac")>=0&&OS.indexOf("os")>0&&OS.indexOf("x")<0;
	}
	
	public static boolean isMacOSX(){
		return OS.indexOf("mac")>=0&&OS.indexOf("os")>0&&OS.indexOf("x")>0;
	}
	
	public static boolean isWindows(){
		return OS.indexOf("windows")>=0;
	}
	
	public static boolean isOS2(){
		return OS.indexOf("os/2")>=0;
	}
	
	public static boolean isSolaris(){
		return OS.indexOf("solaris")>=0;
	}
	
	public static boolean isSunOS(){
		return OS.indexOf("sunos")>=0;
	}
	
	public static boolean isMPEiX(){
		return OS.indexOf("mpe/ix")>=0;
	}
	
	public static boolean isHPUX(){
		return OS.indexOf("hp-ux")>=0;
	}
	
	public static boolean isAix(){
		return OS.indexOf("aix")>=0;
	}
	
	public static boolean isOS390(){
		return OS.indexOf("os/390")>=0;
	}
	
	public static boolean isFreeBSD(){
		return OS.indexOf("freebsd")>=0;
	}
	
	public static boolean isIrix(){
		return OS.indexOf("irix")>=0;
	}
	
	public static boolean isDigitalUnix(){
		return OS.indexOf("digital")>=0&&OS.indexOf("unix")>0;
	}
	
	public static boolean isNetWare(){
		return OS.indexOf("netware")>=0;
	}
	
	public static boolean isOSF1(){
		return OS.indexOf("osf1")>=0;
	}
	
	public static boolean isOpenVMS(){
		return OS.indexOf("openvms")>=0;
	}
	
	/**
	 * 获取操作系统名字
	 * @return 操作系统名
	 */
	public static EPlatform getOSname(){
		if(isAix()){
			_instance.platform = EPlatform.AIX;
		}else if (isDigitalUnix()) {
			_instance.platform = EPlatform.Digital_Unix;
		}else if (isFreeBSD()) {
			_instance.platform = EPlatform.FreeBSD;
		}else if (isHPUX()) {
			_instance.platform = EPlatform.HP_UX;
		}else if (isIrix()) {
			_instance.platform = EPlatform.Irix;
		}else if (isLinux()) {
			_instance.platform = EPlatform.Linux;
		}else if (isMacOS()) {
			_instance.platform = EPlatform.Mac_OS;
		}else if (isMacOSX()) {
			_instance.platform = EPlatform.Mac_OS_X;
		}else if (isMPEiX()) {
			_instance.platform = EPlatform.MPEiX;
		}else if (isNetWare()) {
			_instance.platform = EPlatform.NetWare_411;
		}else if (isOpenVMS()) {
			_instance.platform = EPlatform.OpenVMS;
		}else if (isOS2()) {
			_instance.platform = EPlatform.OS2;
		}else if (isOS390()) {
			_instance.platform = EPlatform.OS390;
		}else if (isOSF1()) {
			_instance.platform = EPlatform.OSF1;
		}else if (isSolaris()) {
			_instance.platform = EPlatform.Solaris;
		}else if (isSunOS()) {
			_instance.platform = EPlatform.SunOS;
		}else if (isWindows()) {
			_instance.platform = EPlatform.Windows;
		}else{
			_instance.platform = EPlatform.Others;
		}
		return _instance.platform;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(OSinfo.getOSname());
	}

}

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


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

  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.   }  
  9. }  
目录
相关文章
|
23天前
|
Java
【Java】如果一个集合中类型是String如何使用拉姆达表达式 进行Bigdecimal类型计算?
【Java】如果一个集合中类型是String如何使用拉姆达表达式 进行Bigdecimal类型计算?
24 0
|
28天前
|
Java
java中的泛型类型擦除
java中的泛型类型擦除
13 2
|
1月前
|
存储 Java
JAVA字符串与其他类型数据的转换
JAVA字符串与其他类型数据的转换
27 4
|
1月前
|
存储 Java fastjson
Java泛型-4(类型擦除后如何获取泛型参数)
Java泛型-4(类型擦除后如何获取泛型参数)
33 1
|
1月前
|
Shell
Flume【问题记录 01】【at org.apache.flume.node.Application.main(Application.java:xxx) 类问题整理+其他类型问题总结】【避坑指南】
【2月更文挑战第17天】Flume【问题记录 01】【at org.apache.flume.node.Application.main(Application.java:xxx) 类问题整理+其他类型问题总结】【避坑指南】
51 2
|
6天前
|
Java
Java 16 新玩法:instanceof 升级版,让类型检查更精准
Java 16 新玩法:instanceof 升级版,让类型检查更精准
11 0
|
6天前
|
存储 监控 安全
泛型魔法:解码Java中的类型参数
泛型魔法:解码Java中的类型参数
26 0
泛型魔法:解码Java中的类型参数
|
27天前
|
存储 Shell Linux
【Shell 命令集合 网络通讯 】⭐Linux 显示当前系统的主机名和操作系统类型 uuname命令 使用教程
【Shell 命令集合 网络通讯 】⭐Linux 显示当前系统的主机名和操作系统类型 uuname命令 使用教程
28 0
|
28天前
|
存储 安全 Java
Java变量类型
Java变量类型
7 0
|
1月前
|
存储 监控 安全
Java基于物联网技术的智慧工地云管理平台源码 依托丰富的设备接口标准库,快速接入工地现场各类型设备
围绕施工安全、质量管理主线,通过物联感知设备全周期、全覆盖实时监测,将管理动作前置,实现从事后被动补救到事前主动预防的转变。例如塔吊运行监测,超重预警,升降机、高支模等机械设备危险监控等,通过安全关键指标设定,全面掌握现场安全情况,防患于未然。
147 5