23

some of my code was failing in x64, I start digging and this is due to some code that calls native stuff via Runtime.getRuntime().exec()...

But this code is probably some years old, it does not take into account newer OS, and some of the code looks like this:

String osName = System.getProperty("os.name");
    if (osName.equals("Windows NT") || osName.equals("Windows 2000") || osName.equals("Windows XP")) {
        cmd = new String[3];
        cmd[0] = WINDOWS_NT_2000_COMMAND_1;
        cmd[1] = WINDOWS_NT_2000_COMMAND_2;
        cmd[2] = command;
    } else if (osName.equals("Windows 95") || osName.equals("Windows 98") || osName.equalsIgnoreCase("Windows ME")) {
        cmd = new String[3];
        cmd[0] = WINDOWS_9X_ME_COMMAND_1;
        cmd[1] = WINDOWS_9X_ME_COMMAND_2;
        cmd[2] = command;

I would like to fix this for all new OSs (w2008, windows 7, ...), but I dont have access to a host of each kind, and I don't want to install in a VM just to see the value, does anybody know of some list somewhere? have not find any yet.

EDIT: I would need: windows 7, windows 2003, windows 2008, windows 2008R2 Also, I am no the 1.6u18 so no worries about the bug some guys mentioned.

Persimmonium
  • 15,593
  • 11
  • 47
  • 78
  • You might be interested into this question: http://stackoverflow.com/questions/1803075/crowdsourcing-a-complete-list-of-common-java-system-properties-and-known-values Unfortunately, I haven't had time to release my findings as a proper open source project though :( – sfussenegger Mar 01 '10 at 17:46
  • 1
    It's not an answer, but this is why you should *always* have a default case. – C. Ross Mar 01 '10 at 17:49
  • To C. Ross, there is an else statement, but anyway it fails as it defaults to thinking its a linux – Persimmonium Mar 01 '10 at 17:53

8 Answers8

13

While this is not a complete solution, you can get a 32-bit JDK and run a simple code printing os.name and os.version with different compatibility settings.

Here're the os.name/os.version values reported by different JDKs on a Windows 8.1 box:

╔═════════════════╤════════════╤════════════╤════════════╤═══════════════╤═══════════════╤══════════════════════╤══════════════════════╗
║ Java/OS version │ Windows 95 │ Windows 98 │ Windows XP │ Windows Vista │ Windows 7     │ Windows 8            │ Windows 8.1          ║
╟─────────────────┼────────────┼────────────┼────────────┼───────────────┼───────────────┼──────────────────────┼──────────────────────╢
║ 1.4.2           │ Windows 95 │ Windows 98 │ Windows XP │ Windows Vista │ Windows Vista │ Windows Vista        │ Windows Vista        ║
║                 │        4.0 │       4.10 │        5.1 │           6.0 │           6.1 │                  6.2 │                  6.2 ║
║ 1.5.0           │ Windows 95 │ Windows 98 │ Windows XP │ Windows Vista │ Windows 7     │ Windows NT (unknown) │ Windows NT (unknown) ║
║                 │        4.0 │       4.10 │        5.1 │           6.0 │           6.1 │                  6.2 │                  6.2 ║
║ 1.6.0           │ Windows 95 │ Windows 98 │ Windows XP │ Windows Vista │ Windows 7     │ Windows 8            │ Windows 8            ║
║                 │        4.0 │       4.10 │        5.1 │           6.0 │           6.1 │                  6.2 │                  6.2 ║
║ 1.7.0           │ Windows 95 │ Windows 98 │ Windows XP │ Windows Vista │ Windows 7     │ Windows 8            │ Windows 8.1          ║
║                 │        4.0 │       4.10 │        5.1 │           6.0 │           6.1 │                  6.2 │                  6.3 ║
║ 1.8.0           │ Windows 95 │ Windows 98 │ Windows XP │ Windows Vista │ Windows 7     │ Windows 8            │ Windows 8.1          ║
║                 │        4.0 │       4.10 │        5.1 │           6.0 │           6.1 │                  6.2 │                  6.3 ║
╚═════════════════╧════════════╧════════════╧════════════╧═══════════════╧═══════════════╧══════════════════════╧══════════════════════╝
Bass
  • 4,977
  • 2
  • 36
  • 82
6

Most likely you could change the code to say

if (osName.equals("Windows 95") || osName.equals("Windows 98") || osName.equalsIgnoreCase("Windows ME")) {
    cmd = new String[3];
    cmd[0] = WINDOWS_9X_ME_COMMAND_1;
    cmd[1] = WINDOWS_9X_ME_COMMAND_2;
    cmd[2] = command;
}
else {
    cmd = new String[3];
    cmd[0] = WINDOWS_NT_2000_COMMAND_1;
    cmd[1] = WINDOWS_NT_2000_COMMAND_2;
    cmd[2] = command;
}
erikkallen
  • 33,800
  • 13
  • 85
  • 120
  • 3
    I'll test a small variation of this: if (osName.equals("Windows 95")...){ //w95 etc} else if (osName.contains("Windows")){ //nt and new ones} else {//unix} – Persimmonium Mar 01 '10 at 22:11
5

I dealt with this at Symantec when Visual Cafe was still alive... I don't recommend doing it this way at all. The problem is that different vendors can supply different strings. I would suggest using an OS specific way to determine the platform.

You could use the "ver" utility on Windows and "uname" on Unix type systems.

It might be better to use "GetNativeSystemInfo" on Windows, but that would require native code.

The reason I suggest that way rather then relying on the System.getProperty method is because you then only have to deal with the underlying OS instead of the JVM sitting on top of the OS - and that removes the issue where different VMs report different things for the same platform.

EDIT: Obviously you would have to try different ways of getting the information as some of them may require running the shell instead of just the command. But if you stick with bash it should be good. Basically try running commands until one of them works... not pretty but it would work.

TofuBeer
  • 60,850
  • 18
  • 118
  • 163
2

No list, but on Windows7, with a JDK6_u18:

os.name = "Windows 7"

Note: there was a bug on JFK6_u14 and before, where it displayed:

"Windows Vista" instead of "Windows 7" (even though the OS was actually "Windows 7"), so be careful!

According to this HowTo, it should be "Windows 2003" for W2003.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

As newer versions should require what the NT line required, it might actually make more sense to check for old versions and else use the NT settings instead of checking for newer versions, something like this:

String osName = System.getProperty("os.name");
if (osName.equals("Windows 95") || osName.equals("Windows 98")
        || osName.equalsIgnoreCase("Windows ME")) {
    cmd = new String[3];
    cmd[0] = WINDOWS_9X_ME_COMMAND_1;
    cmd[1] = WINDOWS_9X_ME_COMMAND_2;
    cmd[2] = command;
} else {
    cmd = new String[3];
    cmd[0] = WINDOWS_NT_2000_COMMAND_1;
    cmd[1] = WINDOWS_NT_2000_COMMAND_2;
    cmd[2] = command;
}
Fabian Steeg
  • 44,988
  • 7
  • 85
  • 112
2

Depends on the version of Java that you're running, I came across this bug:

http://bugs.sun.com/view_bug.do?bug_id=6819886

so as long as you use a latter version of the JDK it should return Windows 7 just fine.

Not sure about Windows Server 2008 though, I'm guessing Windows Server 2008.

There's a reasonably complete list here:

http://mindprod.com/jgloss/properties.html#OSNAME

Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150
0

This code will give you the latest windows OS name like "windows server 2016"

public static String getFullOSName() {
        String cmds ="systeminfo";
        String osName = null;
        try {``
            BufferedReader bufferedreader = executeCommand(cmds);
            String line;
            while ((line = bufferedreader.readLine()) != null) {
                if (line.contains("OS Name")) {
                    String services[] = line.split(":");
                    osName = services[1].trim();
                    return osName;
                }
            }
        } catch (Exception ex) {
           }
        return osName;
    }

    /**
     * Execute Command 
     * 
     * @param command
     * @return
     * @throws Exception
     */

    private static BufferedReader executeCommand(String command) throws Exception {
        BufferedReader bufferedreader = null;
        try {
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec(command);
            InputStream inputstream = proc.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            bufferedreader = new BufferedReader(inputstreamreader);
        } catch (Exception ex) {
            throw new Exception("Command Execution failed on windows. command = " + command);
        }
        return bufferedreader;
    }
User42
  • 970
  • 1
  • 16
  • 27
0

Since this came up as the first hit under a different search context, I'll throw this out there for others who might be searching more generally about this topic:

I use this to determine broad-scope operating systems (windows vs Mac vs Linux, etc.). It's an enum where you leverage the different public static methods from anywhere in your program.

public enum OS {

WIN, MAC, LINUX, SOLARIS, FREEBSD;

private static OS thisOS;

private static void setThisOS() {
    String os = System.getProperty("os.name").toLowerCase();
    if (os.contains("win")) {
        thisOS = OS.WIN;
    }
    else if (os.contains("mac")) {
        thisOS = OS.MAC;
    }
    else if (os.contains("linux")) {
        thisOS = OS.LINUX;
    }
    else if (os.contains("sun")) {
        thisOS = OS.SOLARIS;
    }
    else if (os.contains("free")) {
        thisOS = OS.FREEBSD;
    }
}

public static boolean isWindows() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.WIN);
}

public static boolean isMac() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.MAC);
}

public static boolean isLinux() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.LINUX);
}

public static boolean isWinMac() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.WIN) || thisOS.equals(OS.MAC);
}

public static boolean isSun() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.SOLARIS);
}

public static boolean isBSD() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.FREEBSD);
}

public static boolean isNix() {
    if (thisOS == null) {
        setThisOS();
    }
    return thisOS.equals(OS.LINUX) || thisOS.equals(OS.SOLARIS) || thisOS.equals(OS.FREEBSD);
}

}

Michael Sims
  • 2,360
  • 1
  • 16
  • 29