Detecting Windows or Linux? [duplicate]
Asked Answered
G

5

156

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.

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

Generous answered 11/1, 2013 at 23:13 Comment(1)
"The program needs to do some things differently on each platform." What, specifically?Bop
C
214

apache commons lang has a class SystemUtils.java you can use :

SystemUtils.IS_OS_LINUX
SystemUtils.IS_OS_WINDOWS
Clermontferrand answered 12/1, 2013 at 0:35 Comment(2)
Simply tests whether os.name property begins with Windows...Scraggy
@GeorgeChakhidze This is exactly how software breaks on newer Windows versions...Coryphaeus
E
98

Try:

System.getProperty("os.name");

http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperties%28%29

Engraving answered 11/1, 2013 at 23:25 Comment(1)
See also os.arch & os.version (in case that matters).Bop
C
68

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";
        }
    }
    
}
Complaisance answered 21/7, 2014 at 9:2 Comment(2)
OS.indexOf("...") >= 0 can be replaced with OS.contains("...")Typographer
Should probably use: System.getProperty("os.name").toLowerCase(Locale.ROOT);Votaw
S
31

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");
    }
}
Shiff answered 8/5, 2015 at 19:6 Comment(2)
if(File.separatorChar == '\\') windows = true;Laurenlaurena
Or just SystemUtils.IS_OS_WINDOWS.Fujio
O
12

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=" + s);
  }
}

// EXAMPLE OUTPUT: OS=name: Windows 7, version: 6.1, arch: amd64

Here are more details:

Ogee answered 28/8, 2013 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.