finding the os

is there someway for java to know which OS it is running on ( ie linux or windows)?
Who is Participating?
CEHJConnect With a Mentor Commented:
Stirng os = System.getProperty("");
Bart CremersJava ArchitectCommented:
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("");
        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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.