0

There are parts of the program that are affected by OS type. so i try to branch by os name as below. when I run it locally,it works. but I run it as a client program after uploading it to the server, it seems that it can not get the os name. Please let me know how can i get the os name from the client program. thanks.

public void getOSName() {   
   String osName = System.getProperty("os.name")
   if(!osName.trim().toUpperCase().equals("WINDOWS 10")){
   run();        
   }else{
   }
}
Shashwat
  • 2,342
  • 1
  • 17
  • 33
  • 1
    When you say "it seems that it cannot get the os name", how exactly did you try to do that? Some code would be helpful so whatever is wrong could be pointed out. – Vada Poché Jul 05 '19 at 05:48
  • so ... code that you run on a server can't get the system properties of code that runs on a client? seems logical to me. – Stultuske Jul 05 '19 at 06:01
  • This is rather unclear. What code are you running on the server? What code is running on the client? What has this code to do with SWT? – greg-449 Jul 05 '19 at 06:34

1 Answers1

3

Checkout below util class for OS validation.

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;
}
Shashwat
  • 2,342
  • 1
  • 17
  • 33
  • Thank you for your answer. but when i use System.getProperty("os.name") then "windows NT" is returned. Even if i'm using window 10 pro. Do you know what problem it is? – user11663428 Jul 08 '19 at 08:58
  • Which version of Java you are using checkout : https://bugs.openjdk.java.net/browse/JDK-8066504, https://bugs.openjdk.java.net/browse/JDK-8066656 and https://bugs.java.com/bugdatabase/view_bug.do?bug_id=7170169 as issues are in resolved state. I also updated my answer. – Shashwat Jul 09 '19 at 05:18