java - 如何从客户端程序获取操作系统名称?

标签 java swt getproperty

程序的某些部分受操作系统类型的影响。 所以我尝试按操作系统名称进行分支,如下所示。 当我在本地运行它时,它有效。但我将其上传到服务器后作为客户端程序运行,似乎无法获取操作系统名称。 请让我知道如何从客户端程序获取操作系统名称。 谢谢。

public void getOSName() {   
   String osName = System.getProperty("os.name")
   if(!osName.trim().toUpperCase().equals("WINDOWS 10")){
   run();        
   }else{
   }
}

最佳答案

检查下面的 util 类以进行操作系统验证。

Using System properties : Due to this issue this approach may fail. Please see Java's “os.name” for Windows 10?

/**
 * The Class OSValidator.
 */
public final class OSValidator {

    /** The Constant OS. */
    public static final String OS = System.getProperty("os.name").toLowerCase();

    /**
     * Checks if is windows 7.
     *
     * @return true, if is windows 7
     */
    public static final boolean isWindows7() {
        return (OS.indexOf("windows 7") >= 0);
    }

    /**
     * Checks if is windows 10.
     *
     * @return true, if is windows 10
     */
    public static final boolean isWindows10() {
        return (OS.indexOf("windows 10") >= 0);
    }

    /**
     * Checks if is mac.
     *
     * @return true, if is mac
     */
    public static final boolean isMac() {
        return (OS.indexOf("mac") >= 0);
    }
}

Using SystemUtils – Apache Commons Lang

public String getOperatingSystemSystemUtils() {
    String os = SystemUtils.OS_NAME;
    // System.out.println("Using SystemUtils: " + os);
    return os;
}

关于java - 如何从客户端程序获取操作系统名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56897149/

相关文章:

java - SWT JFace如何在moveBelow()后重绘

java - 在 Properties 实例上调用 getProperty 将返回 null 而不是 value

java - 如何从 SOAP 响应中获取单个元素的值? ( java )

java - 在 sigar java api 中获取进程开始时间作为 future 日期

java - 使用透明蒙版将 SWT 图像保存到文件

Java - System.getProperty 在最终类属性的初始化中返回 null,但在方法中不返回 null

c# - (C#) 如何根据name字段存值?

java - 如何在 ArrayList 上编写 for 循环来查找最大值

java - Android:无法正确创建共享 kml(xml) 文件的 Intent

java - 如何以编程方式从颜色名称获取 R-G-B 值