Link to home
Start Free TrialLog in
Avatar of angelblade27
angelblade27

asked on

finding the os

is there someway for java to know which OS it is running on ( ie linux or windows)?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
:-)