How do I get the os name from the client program?

400 Views Asked by At

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{
   }
}
1

There are 1 best solutions below

2
Shashwat On

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