Java 判断操作系统类型(适用于各种操作系统)
最近一段时间写一个授权的程序,需要获取很多信息来保证程序不能随意复制使用,必须经过授权才可以。
为了限制用户使用的操作系统,必须有统一的方法来获取才可以。
在JAVA中,通过System.getProperty("os.name")来获取,通过参考:http://lopica.sourceforge.net/os.html 来实现各操作系统的判断。
针对windows系统,这里不具体判断是那个版本,如果有需要,可以在判断出windows之后,继续判断,判断需要考虑java的版本,版本不同,结果也不一样。
下面上代码:
1.枚举类型:EPlatform
/*** 平台
*@authorisea533*/
public enumEPlatform {
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");privateEPlatform(String desc){this.description =desc;
}publicString toString(){returndescription;
}privateString description;
}
2.操作系统类:OSinfo
/*** 操作系统类:
* 获取System.getProperty("os.name")对应的操作系统
*@authorisea533*/
public classOSinfo {private static String OS = System.getProperty("os.name").toLowerCase();private static OSinfo _instance = newOSinfo();privateEPlatform platform;privateOSinfo(){}public static booleanisLinux(){return OS.indexOf("linux")>=0;
}public static booleanisMacOS(){return OS.indexOf("mac")>=0&&OS.indexOf("os")>0&&OS.indexOf("x")<0;
}public static booleanisMacOSX(){return OS.indexOf("mac")>=0&&OS.indexOf("os")>0&&OS.indexOf("x")>0;
}public static booleanisWindows(){return OS.indexOf("windows")>=0;
}public static booleanisOS2(){return OS.indexOf("os/2")>=0;
}public static booleanisSolaris(){return OS.indexOf("solaris")>=0;
}public static booleanisSunOS(){return OS.indexOf("sunos")>=0;
}public static booleanisMPEiX(){return OS.indexOf("mpe/ix")>=0;
}public static booleanisHPUX(){return OS.indexOf("hp-ux")>=0;
}public static booleanisAix(){return OS.indexOf("aix")>=0;
}public static booleanisOS390(){return OS.indexOf("os/390")>=0;
}public static booleanisFreeBSD(){return OS.indexOf("freebsd")>=0;
}public static booleanisIrix(){return OS.indexOf("irix")>=0;
}public static booleanisDigitalUnix(){return OS.indexOf("digital")>=0&&OS.indexOf("unix")>0;
}public static booleanisNetWare(){return OS.indexOf("netware")>=0;
}public static booleanisOSF1(){return OS.indexOf("osf1")>=0;
}public static booleanisOpenVMS(){return OS.indexOf("openvms")>=0;
}
/*** 获取操作系统名字
*@return操作系统名*/
public staticEPlatform 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;
}/***@paramargs*/
public static voidmain(String[] args) {
System.out.println(OSinfo.getOSname());
}
}
我使用的Windows 7 识别出来:Windows ,如果大家使用别的操作系统,希望能把操作系统和结果在这里留言写下来。
如果结果错误,你可以使用下面的代码获取你的操作系统信息:
classWhatOS
{public static voidmain( String args[] )
{
System.out.println( System.getProperty("os.name") );
System.out.println( System.getProperty("os.version") );
System.out.println( System.getProperty("os.arch") );
}
}
来自: http://blog.csdn.net/isea533/article/details/8449919