Java 判斷作業系統型別(適用於各種作業系統)--轉載
阿新 • • 發佈:2019-02-13
最近一段時間寫一個授權的程式,需要獲取很多資訊來保證程式不能隨意複製使用,必須經過授權才可以。
為了限制使用者使用的作業系統,必須有統一的方法來獲取才可以。
在JAVA中,通過System.getProperty("os.name")
來獲取,通過參考:http://lopica.sourceforge.net/os.html 來實現各作業系統的判斷。
針對windows系統,這裡不具體判斷是那個版本,如果有需要,可以在判斷出windows之後,繼續判斷,判斷需要考慮java的版本,版本不同,結果也不一樣。
下面上程式碼:
1.列舉型別:EPlatform
/**
* 平臺
*/
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
/**
* 作業系統類:
* 獲取System.getProperty("os.name")對應的作業系統
*/
public class OSinfo {
private static final 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());
}
}
如果結果錯誤,你可以使用下面的程式碼獲取你的作業系統資訊:
public class WhatOS {
public static void main(String args[]) {
System.out.println(System.getProperty("os.name"));//Windows 8.1(不準,我的系統是Windows10)
System.out.println(System.getProperty("os.version"));//6.3
System.out.println(System.getProperty("os.arch"));//amd64
}
}