Java check windows or linux

How do I programmatically determine operating system in Java?

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?

22 Answers 22

P.S. You may find this code useful:

All it does is print out all the properties provided by your Java implementations. It’ll give you an idea of what you can find out about your Java environment via properties. 🙂

As indicated in other answers, System.getProperty provides the raw data. However, the Apache Commons Lang component provides a wrapper for java.lang.System with handy properties like SystemUtils.IS_OS_WINDOWS , much like the aforementioned Swingx OS util.

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?»

I agree with the getOSName function, on the basis of OAOO (once and only once); however, the caching is totally redundant given the speed of hash lookups.

Totally redundant might be a bit harsh, hash lookups are more expensive than accessing a reference. It all depends on the context.

I reread this answer. If you are going to cache, cache the values of isWindows , isUnix , etc. That way you save on the string comparison time also.

some of the links in the answers above seem to be broken. I have added pointers to current source code in the code below and offer an approach for handling the check with an enum as an answer so that a switch statement can be used when evaluating the result:

OsCheck.OSType ostype=OsCheck.getOperatingSystemType(); switch (ostype)
/** * 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; > > 

The code above may have locale issues, since it uses toLowerCase(), which is locale sensitive. Where this matters is particularly when converting i’s to lower/upper case, since in Turkey, I becomes lower case undotted i (ı), and i becomes upper case dotted i (İ). So «WINDOWS».toLowerCase().indexOf(«win») will return -1 in Turkey. Always pass a locale when doing a lower case of a particular language, ie «WINDOWS».toLowerCase(Locale.ENGLISH).indexOf(«win») will work in Turkey.

Читайте также:  Размеры всех файлов linux

The following JavaFX classes have static methods to determine current OS (isWindows(),isLinux(). ):

  • com.sun.javafx.PlatformUtil
  • com.sun.media.jfxmediaimpl.HostUtils
  • com.sun.javafx.util.Utils

Please note that the access to «com/sun/javafx/*» is discouraged now (checked it with JDK 1.8.0_121).

@HummelingEngineeringBV: I guess it was a mistake from my side. I am working with eclipse Neon 4.6.3 and the «Java Build Path» shows several «Discouraged: com/sun/javafx/**» warnings. However, as I found out, this happens to be an eclipse-bug and/or -feature (see link).

I have to correct myself one more time. Beginning with Java 9/10+, several «com.sun.*» packages/APIs are about to be removed. Check out this link for more info. I actually stumbled over this because we use some of these packages. Migrating to eclipse 4.8/JDK 10, we now have to fix these and several other compiler errors due to missing references.

For accessing OS use: System.getProperty(«os.name») .

But WAIT.

Why not create a utility class, make it reusable! And probably much faster on multiple calls. Clean, clear, faster!

Create a Util class for such utility functions. Then create public enums for each operating system type.

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; > > 

Now, you can easily invoke class from any class as follows,(P.S. Since we declared os variable as static, it will consume time only once to identify the system type, then it can be used until your application halts. )

Читайте также:  Linux ubuntu установить gnome

I want to use this piece of code in an open-source project (github.com/openhab/openhab-addons), is this okay with you?

Go ahead and put rationale for why you said so. I am happy to change the code if you have any reasonable evidence. Be reasonable and clarify your comment please, so that I can improve my 7-8 years old answer.

A small example of what you’re trying to achieve would probably be a class similar to what’s underneath:

import java.util.Locale; public class OperatingSystem < private static String OS = System.getProperty("os.name", "unknown").toLowerCase(Locale.ROOT); public static boolean isWindows() < return OS.contains("win"); >public static boolean isMac() < return OS.contains("mac"); >public static boolean isUnix() < return OS.contains("nux"); >> 

This particular implementation is quite reliable and should be universally applicable. Just copy and paste it into your class of choice.

Why make it so complicated? System.getProperty(«os.name») is sufficient. Don’t bother with the unnecessary fluff.

System.getProperty("os.name"); System.getProperty("os.version"); System.getProperty("os.arch"); 

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:

also suffers from the same issue identified by James Roper in Wolfgang Fahl’s answer — use of toLowerCase without specifying a locale

I think following can give broader coverage in fewer lines

import org.apache.commons.exec.OS; if (OS.isFamilyWindows()) < //load some property >else if (OS.isFamilyUnix()) < //load some other property >

If you’re working in a security sensitive environment, then please read this through.

Please refrain from ever trusting a property obtained via the System#getProperty(String) subroutine! Actually, almost every property including os.arch , os.name , and os.version isn’t readonly as you’d might expect — instead, they’re actually quite the opposite.

First of all, any code with sufficient permission of invoking the System#setProperty(String, String) subroutine can modify the returned literal at will. However, that’s not necessarily the primary issue here, as it can be resolved through the use of a so called SecurityManager , as described in greater detail over here.

The actual issue is that any user is able to edit these properties when running the JAR in question (through -Dos.name= , -Dos.arch= , etc.). A possible way to avoid tampering with the application parameters is by querying the RuntimeMXBean as shown here. The following code snippet should provide some insight into how this may be achieved.

RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); List arguments = runtimeMxBean.getInputArguments(); for (String argument : arguments) < if (argument.startsWith("-Dos.name") < // System.getProperty("os.name") altered >else if (argument.startsWith("-Dos.arch") < // System.getProperty("os.arch") altered >> 

Источник

Detecting Windows or Linux?

I am seeking to run a common Java program in both Windows and Linux.

The program needs to do some things differently on each platform.

Читайте также:  Linux tools on mac

So how can / should my Java program detect it is running under Linux vs. Windows?

Java Solutions

Solution 1 — Java

SystemUtils.IS_OS_LINUX SystemUtils.IS_OS_WINDOWS 

Solution 2 — Java

Solution 3 — Java

you can use this Useful simple class:

public class OSValidator < private static String OS = System.getProperty("os.name").toLowerCase(); public static void main(String[] args) < System.out.println(OS); if (isWindows()) < System.out.println("This is Windows"); > else if (isMac()) < System.out.println("This is MacOS"); > else if (isUnix()) < System.out.println("This is Unix or Linux"); > else if (isSolaris()) < System.out.println("This is Solaris"); > else < System.out.println("Your OS is not supported!!"); > > public static boolean isWindows( ) < return OS.contains("win"); > public static boolean isMac( ) < return OS.contains("mac"); > public static boolean isUnix( ) < return (OS.contains("nix") || OS.contains("nux") || OS.contains("aix")); > public static boolean isSolaris( ) < return OS.contains("sunos"); > public static String getOS( )< if (isWindows()) < return "win"; > else if (isMac()) < return "osx"; > else if (isUnix()) < return "uni"; > else if (isSolaris()) < return "sol"; > else < return "err"; > > > 

Solution 4 — Java

I think It’s a best approach to use Apache lang dependency to decide which OS you’re running programmatically through Java

import org.apache.commons.lang3.SystemUtils; public class App < public static void main( String[] args ) < if(SystemUtils.IS_OS_WINDOWS_7) System.out.println("It's a Windows 7 OS"); if(SystemUtils.IS_OS_WINDOWS_8) System.out.println("It's a Windows 8 OS"); if(SystemUtils.IS_OS_LINUX) System.out.println("It's a Linux OS"); if(SystemUtils.IS_OS_MAC) System.out.println("It's a MAC OS"); > > 

Solution 5 — Java

You can use «system.properties.os», for example:

public class GetOs < public static void main (String[] args) < String s = "name: " + System.getProperty ("os.name"); s += ", version: " + System.getProperty ("os.version"); s += ", arch: " + System.getProperty ("os.arch"); System.out.println ("OS hljs-comment">// EXAMPLE OUTPUT: OS=name: Windows 7, version: 6.1, arch: amd64 

Источник

java check if current operating system is linux or windows

Java check if current operating system is linux or windows

TylerH

TylerH

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public class SystemUtils public static void main(String[] args) // if we run this util on Linux eg Ubuntu, Debian, CentOS System.out.println(isLinux()); // true > public static boolean isLinux() String name = System.getProperty("os.name"); return name.toLowerCase().contains("linux"); > public static boolean isWindows() String name = System.getProperty("os.name"); return name.toLowerCase().contains("win"); > >

Our content is created by volunteers — like Wikipedia. If you think, the things we do are good, donate us. Thanks!

Источник

Оцените статью
Adblock
detector