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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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:
http://svn.terracotta.org/svn/tc/dso/trunk/code/base/common/src/com/tc/util/runtime/And you can see a similar class to handle JVM versions (Vm.java and VmVersion.java) here:
Taken from this project https://github.com/RishiGupta12/serial-communication-manager
Try this,simple and easy
Oct. 2008:
I would recommend to cache it in a static variable:
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?"
You can get the OS type name by using
You can get all the info related to OS by:
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=