Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does System.getProperty("os.name") return in latest Windows OSs

Tags:

java

windows

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.

like image 384
Persimmonium Avatar asked Mar 01 '10 17:03

Persimmonium


People also ask

How to check system os in Java?

Use the System. getProperty() method in Java to get the Operating System name.

How does system getProperty work in Java?

The getProperty(String key) method in Java is used to returns the system property denoted by the specified key passed as its argument.It is a method of the java. lang. System Class. where key is the name of the System property.

What is system property in Java?

System properties include information about the current user, the current version of the Java runtime, and the character used to separate components of a file path name. The following table describes some of the most important system properties.


4 Answers

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 ║
╚═════════════════╧════════════╧════════════╧════════════╧═══════════════╧═══════════════╧══════════════════════╧══════════════════════╝
like image 138
Bass Avatar answered Oct 17 '22 08:10

Bass


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;
}
like image 34
erikkallen Avatar answered Oct 17 '22 08:10

erikkallen


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.

like image 5
TofuBeer Avatar answered Oct 17 '22 07:10

TofuBeer


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.

like image 2
VonC Avatar answered Oct 17 '22 07:10

VonC