我想确定我的Java程序以编程方式运行的主机的操作系统(例如:我希望能够根据我是在Windows还是Unix平台上加载不同的属性).100%可靠性最安全的方法是什么?
您可以使用:
System.getProperty("os.name")
PS您可能会发现此代码很有用:
class ShowProperties { public static void main(String[] args) { System.getProperties().list(System.out); } }
它所做的就是打印出Java实现提供的所有属性.它将通过属性为您提供有关Java环境的信息.:-)
如其他答案所示,System.getProperty提供原始数据.但是,Apache Commons Lang组件为java.lang.System提供了一个包装器,它具有SystemUtils.IS_OS_WINDOWS等方便的属性,就像前面提到的Swingx OS util一样.
2008年10月:
我建议将其缓存在一个静态变量中:
public static final class OsUtils { private static String OS = null; public static String getOsName() { if(OS == null) { OS = System.getProperty("os.name"); } return OS; } public static boolean isWindows() { return getOsName().startsWith("Windows"); } public static boolean isUnix() // and so on }
这样,每次你要求Os时,你都不会在应用程序的生命周期内多次获取属性.
2016年2月:7年后:
Windows 10存在一个错误(原始答案时不存在).
请参阅" Java的"os.name"for Windows 10? "
上面答案中的一些链接似乎被打破了.我在下面的代码中添加了指向当前源代码的指针,并提供了一种处理检查的方法,将枚举作为答案,以便在评估结果时可以使用switch语句:
OsCheck.OSType ostype=OsCheck.getOperatingSystemType(); switch (ostype) { case Windows: break; case MacOS: break; case Linux: break; case Other: break; }
帮助程序类是:
/** * helper class to check the operating system this Java VM runs in * * please keep the notes below as a pseudo-license * * http://stackoverflow.com/questions/228477/how-do-i-programmatically-determine-operating-system-in-java * compare to http://svn.terracotta.org/svn/tc/dso/tags/2.6.4/code/base/common/src/com/tc/util/runtime/Os.java * http://www.docjar.com/html/api/org/apache/commons/lang/SystemUtils.java.html */ import java.util.Locale; public static final class OsCheck { /** * types of Operating Systems */ public enum OSType { Windows, MacOS, Linux, Other }; // cached result of OS detection protected static OSType detectedOS; /** * detect the operating system from the os.name System property and cache * the result * * @returns - the operating system detected */ public static OSType getOperatingSystemType() { if (detectedOS == null) { String OS = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH); if ((OS.indexOf("mac") >= 0) || (OS.indexOf("darwin") >= 0)) { detectedOS = OSType.MacOS; } else if (OS.indexOf("win") >= 0) { detectedOS = OSType.Windows; } else if (OS.indexOf("nux") >= 0) { detectedOS = OSType.Linux; } else { detectedOS = OSType.Other; } } return detectedOS; } }
以下JavaFX类具有用于确定当前OS的静态方法(isWindows(),isLinux()...):
com.sun.javafx.PlatformUtil
com.sun.media.jfxmediaimpl.HostUtils
com.sun.javafx.util.Utils
例:
if (PlatformUtil.isWindows()){ ... }
假设您有这样的实用程序函数的Util类.然后为每种操作系统类型创建公共枚举.
public class Util { public enum OS { WINDOWS, LINUX, MAC, SOLARIS };// Operating systems. private static OS os = null; public static OS getOS() { if (os == null) { String operSys = System.getProperty("os.name").toLowerCase(); if (operSys.contains("win")) { os = OS.WINDOWS; } else if (operSys.contains("nix") || operSys.contains("nux") || operSys.contains("aix")) { os = OS.LINUX; } else if (operSys.contains("mac")) { os = OS.MAC; } else if (operSys.contains("sunos")) { os = OS.SOLARIS; } } return os; } }
然后你可以轻松地从任何类调用类,如下所示(PS因为我们将os变量声明为静态,它只消耗一次时间来识别系统类型,然后它可以在你的应用程序停止之前使用.)
switch (Util.getOS()) { case WINDOWS: //do windows stuff break; case LINUX:
等等...
private static String OS = System.getProperty("os.name").toLowerCase(); public static void detectOS() { if (isWindows()) { } else if (isMac()) { } else if (isUnix()) { } else { } } private static boolean isWindows() { return (OS.indexOf("win") >= 0); } private static boolean isMac() { return (OS.indexOf("mac") >= 0); } private static boolean isUnix() { return (OS.indexOf("nux") >= 0); }
如果你对开源项目如何做这样的事情感兴趣,你可以看看处理这个垃圾的Terracotta类(Os.java):
http://svn.terracotta.org/svn/tc/dso/trunk/code/base/common/src/com/tc/util/runtime/
http://svn.terracotta.org/svn/tc/dso/tags/2.6.4/code/base/common/src/com/tc/util/runtime/
你可以在这里看到一个类似的类来处理JVM版本(Vm.java和VmVersion.java):
http://svn.terracotta.org/svn/tc/dso/trunk/common/src/main/java/com/tc/util/runtime/
试试这个,简单易行
System.getProperty("os.name"); System.getProperty("os.version"); System.getProperty("os.arch");
取自这个项目https://github.com/RishiGupta12/serial-communication-manager
String osName = System.getProperty("os.name"); String osNameMatch = osName.toLowerCase(); if(osNameMatch.contains("linux")) { osType = OS_LINUX; }else if(osNameMatch.contains("windows")) { osType = OS_WINDOWS; }else if(osNameMatch.contains("solaris") || osNameMatch.contains("sunos")) { osType = OS_SOLARIS; }else if(osNameMatch.contains("mac os") || osNameMatch.contains("macos") || osNameMatch.contains("darwin")) { osType = OS_MAC_OS_X; }else { }
我发现Swingx的OS Utils可以完成这项工作.
下面的代码显示了您可以从System API获得的值,这些是您可以通过此API获得的所有内容.
public class App { public static void main( String[] args ) { //Operating system name System.out.println(System.getProperty("os.name")); //Operating system version System.out.println(System.getProperty("os.version")); //Path separator character used in java.class.path System.out.println(System.getProperty("path.separator")); //User working directory System.out.println(System.getProperty("user.dir")); //User home directory System.out.println(System.getProperty("user.home")); //User account name System.out.println(System.getProperty("user.name")); //Operating system architecture System.out.println(System.getProperty("os.arch")); //Sequence used by operating system to separate lines in text files System.out.println(System.getProperty("line.separator")); System.out.println(System.getProperty("java.version")); //JRE version number System.out.println(System.getProperty("java.vendor.url")); //JRE vendor URL System.out.println(System.getProperty("java.vendor")); //JRE vendor name System.out.println(System.getProperty("java.home")); //Installation directory for Java Runtime Environment (JRE) System.out.println(System.getProperty("java.class.path")); System.out.println(System.getProperty("file.separator")); } }
回答: -
Windows 7 6.1 ; C:\Users\user\Documents\workspace-eclipse\JavaExample C:\Users\user user amd64 1.7.0_71 http://java.oracle.com/ Oracle Corporation C:\Program Files\Java\jre7 C:\Users\user\Documents\workspace-Eclipse\JavaExample\target\classes \
我认为以下内容可以在更少的行中提供更广泛的覆盖范围
import org.apache.commons.exec.OS; if (OS.isFamilyWindows()){ //load some property } else if (OS.isFamilyUnix()){ //load some other property }
此处有更多详细信息:https : //commons.apache.org/proper/commons-exec/apidocs/org/apache/commons/exec/OS.html
String osName = System.getProperty("os.name"); System.out.println("Operating system " + osName);