We help IT Professionals succeed at work.

finding the os

186 Views
Last Modified: 2010-03-30
is there someway for java to know which OS it is running on ( ie linux or windows)?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Bart CremersJava Architect

Commented:
private static OperatingSystem os;

public static OperatingSystem getOperatingSystem() {
    if (os != null)
        return os;

    if (System.getProperty("mrj.version") != null) {
        os = new MacOS();
    } else {
        String osName = System.getProperty("os.name");
        if (osName.indexOf("Windows") != -1) {
            os = new Windows();
        } else if(osName.indexOf("OS/2") != -1) {
            os = new OS2();
        } else if(osName.indexOf("VMS") != -1) {
            os = new VMS();
        } else {
            os = new Unix();
        }
    }
    return os;
}


Of course you can return enums, or constants in stead of classes implementing OperatingSystem, but it's just better practice if you want to define custom behaviour depending on the OS.
CERTIFIED EXPERT
Top Expert 2016

Commented:
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.