检测 Windows 或 Linux?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14288185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:38:20  来源:igfitidea点击:

Detecting Windows or Linux?

javalinuxwindowsplatform-detection

提问by Ron Whites

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

我正在寻求在 Windows 和 Linux 中运行一个通用的 Java 程序。

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?

那么我的 Java 程序如何/应该如何检测它是在 Linux 还是 Windows 下运行?

回答by othman

apache commons langhas a class SystemUtils.javayou can use :

apache commons lang有一个类SystemUtils.java你可以使用:

SystemUtils.IS_OS_LINUX
SystemUtils.IS_OS_WINDOWS

回答by paulsm4

You can use "system.properties.os", for example:

您可以使用“system.properties.os”,例如:

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:

以下是更多详细信息:

回答by Domenico Monaco

Useful simple class are forked by me on: https://gist.github.com/kiuz/816e24aa787c2d102dd0

有用的简单类由我分叉:https: //gist.github.com/kiuz/816e24aa787c2d102dd0

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 Mac");
        } 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 support!!");
        }
    }

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

}

回答by Domenico Monaco

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

我认为这是使用 Apache lang 依赖项来决定您通过 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");
    }
}