finding the os

Posted on 2007-10-10
Last Modified: 2010-03-30
is there someway for java to know which OS it is running on ( ie linux or windows)?
Question by:angelblade27
    LVL 86

    Accepted Solution

    Stirng os = System.getProperty("");
    LVL 13

    Expert Comment

    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.
    LVL 86

    Expert Comment

    LVL 86

    Expert Comment


    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    maxMirror challenge 10 70
    wordsCount challenge 10 49
    java set up 1 37
    What is the latest versions eclipse neon 2 63
    Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now