【问题标题】:Detecting Windows or Linux? [duplicate]检测 Windows 还是 Linux? [复制]
【发布时间】:2012-12-26 15:02:31
【问题描述】:

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

程序需要在每个平台上做一些不同的事情。

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

【问题讨论】:

  • “程序需要在每个平台上做一些不同的事情。”具体是什么?

标签: java linux windows platform-detection


【解决方案1】:

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

SystemUtils.IS_OS_LINUX
SystemUtils.IS_OS_WINDOWS

【讨论】:

  • 这更容易,并且可能经过测试!
  • 简单测试os.name属性是否以Windows开头...
  • @GeorgeChakhidze 这正是软件在较新的 Windows 版本上崩溃的方式......
【解决方案2】:

【讨论】:

【解决方案3】:

你可以使用这个有用的简单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";
        }
    }
    
}

【讨论】:

  • OS.indexOf("...") >= 0 可以替换为OS.contains("...")
  • 应该可能使用:System.getProperty("os.name").toLowerCase(Locale.ROOT);
【解决方案4】:

我认为这是使用 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");
    }
}

【讨论】:

  • if(File.separatorChar == '\\') windows = true;
  • 或者只是SystemUtils.IS_OS_WINDOWS
【解决方案5】:

可以使用“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

这里有更多细节:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-13
    • 2014-07-14
    • 2011-05-11
    • 2017-04-29
    • 2011-05-19
    • 2011-06-09
    • 2012-01-29
    • 1970-01-01
    相关资源
    最近更新 更多