?
Solved

finding the os

Posted on 2007-10-10
4
Medium Priority
?
174 Views
Last Modified: 2010-03-30
is there someway for java to know which OS it is running on ( ie linux or windows)?
0
Comment
Question by:angelblade27
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 20050123
Stirng os = System.getProperty("os.name");
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20050880
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20051007
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20054691
:-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 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