How do I programmatically determine operating syst

2018-12-31 10:16发布

I would like to determine the operating system of the host that my Java program is running programmatically (for example: I would like to be able to load different properties based on whether I am on a Windows or Unix platform). What is the safest way to do this with 100% reliability?

标签: java
18条回答
像晚风撩人
2楼-- · 2018-12-31 10:48

If you're interested in how an open source project does stuff like this, you can check out the Terracotta class (Os.java) that handles this junk here:

And you can see a similar class to handle JVM versions (Vm.java and VmVersion.java) here:

查看更多
无与为乐者.
3楼-- · 2018-12-31 10:48

Taken from this project 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 {
}
查看更多
无与为乐者.
4楼-- · 2018-12-31 10:48

Try this,simple and easy

System.getProperty("os.name");
System.getProperty("os.version");
System.getProperty("os.arch");
查看更多
只靠听说
5楼-- · 2018-12-31 10:52

Oct. 2008:

I would recommend to cache it in a static variable:

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
}

That way, every time you ask for the Os, you do not fetch the property more than once in the lifetime of your application.


February 2016: 7+ years later:

There is a bug with Windows 10 (which did not exist at the time of the original answer).
See "Java's “os.name” for Windows 10?"

查看更多
时光乱了年华
6楼-- · 2018-12-31 10:52
String osName = System.getProperty("os.name");
System.out.println("Operating system " + osName);
查看更多
皆成旧梦
7楼-- · 2018-12-31 10:59

You can get the OS type name by using

System.out.println(System.getProperty("os.name"));

You can get all the info related to OS by:

public class MyFirstJavaProgram {

   public static void main(String []args) {
      System.getProperties().list(System.out);    } }

It will output very much details

-- listing properties --

java.runtime.name=OpenJDK Runtime Environment

sun.boot.library.path=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0...

java.vm.version=25.65-b01

java.vm.vendor=Oracle Corporation

java.vendor.url=http://java.oracle.com/

path.separator=:

java.vm.name=OpenJDK 64-Bit Server VM

file.encoding.pkg=sun.io

user.country=US

sun.java.launcher=SUN_STANDARD

sun.os.patch.level=unknown

java.vm.specification.name=Java Virtual Machine Specification

user.dir=/web/com/1502258867_87863

java.runtime.version=1.8.0_65-b17

java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment

java.endorsed.dirs=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0...

os.arch=amd64

java.io.tmpdir=/tmp

line.separator=

java.vm.specification.vendor=Oracle Corporation

os.name=Linux

sun.jnu.encoding=UTF-8

java.library.path=/home/cg/root/GNUstep/Library/Librari...

java.specification.name=Java Platform API Specification

java.class.version=52.0

sun.management.compiler=HotSpot 64-Bit Tiered Compilers

os.version=3.10.0-327.4.4.el7.x86_64

user.home=/usr/share/httpd

user.timezone=

java.awt.printerjob=sun.print.PSPrinterJob

file.encoding=UTF-8

java.specification.version=1.8

user.name=apache

java.class.path=/home/cg/root/GNUstep/Library/Librari...

java.vm.specification.version=1.8

sun.arch.data.model=64

java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0...

sun.java.command=MyFirstJavaProgram

java.specification.vendor=Oracle Corporation

user.language=en

awt.toolkit=sun.awt.X11.XToolkit

java.vm.info=mixed mode

java.version=1.8.0_65

java.ext.dirs=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0...

sun.boot.class.path=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0...

java.vendor=Oracle Corporation

file.separator=/

java.vendor.url.bug=http://bugreport.sun.com/bugreport/

sun.cpu.endian=little

sun.io.unicode.encoding=UnicodeLittle

sun.cpu.isalist=

查看更多
登录 后发表回答