【问题标题】:How do I get the os name from the client program?如何从客户端程序中获取操作系统名称?
【发布时间】:2019-11-15 17:44:28
【问题描述】:

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

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

【问题讨论】:

  • 当您说“它似乎无法获取操作系统名称”时,您究竟是如何尝试这样做的?一些代码会很有帮助,因此可以指出任何错误。
  • so ...您在服务器上运行的代码无法获取在客户端上运行的代码的系统属性?在我看来是合乎逻辑的。
  • 这还不清楚。你在服务器上运行什么代码?什么代码在客户端上运行?这段代码和 SWT 有什么关系?

标签: java swt getproperty


【解决方案1】:

在 util 类下方结帐以进行操作系统验证。

使用系统属性:由于issue,这种方法可能会失败。请看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);
    }
}

使用 SystemUtils – Apache Commons Lang

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-08-05
  • 2020-08-25
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
  • 1970-01-01
相关资源
最近更新 更多