Java中如何检测操作系统
时间:2020-02-23 14:34:18 来源:igfitidea点击:
在本教程中,我们将看到如何检测我们正在使用的操作系统。
我们可以使用system.getProperty("os.name")来获取当前的操作系统名称。
有时,我们需要基于操作系统编写逻辑,因此我们可以使用此逻辑来检测当前操作系统。
Java程序:
package org.igi.theitroad;
/*
* @Author igi Mandliya
*/
public class getHomeDirectoryMain {
public static void main(String[] args)
{
String osName="";
//Getting OS name
osName = System.getProperty("os.name");
System.out.println("--------------");
System.out.println(osName);
if (osName.toLowerCase().indexOf("win") >= 0) {
System.out.println("You are using Windows");
} else if (osName.toLowerCase().indexOf("mac") >= 0) {
System.out.println("You are using MAC");
} else if (osName.toLowerCase().indexOf("nix") >= 0 || osName.toLowerCase().indexOf("nux") >= 0 || osName.toLowerCase().indexOf("aix") > 0 ) {
System.out.println("You are using Unix or Linux");
} else if (osName.toLowerCase().indexOf("sunos") >= 0) {
System.out.println("You are using Solaris");
} else {
System.out.println("Not able to detect OS");
}
System.out.println("--------------");
}
}
我正在使用Mac OS运行上面的程序。
当我运行上上面的程序时,我得到了下面的输出
------------- Mac OS X You are using MAC -------------

