tags:

views:

693

answers:

6

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.

A: 

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  A: 

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
A: 

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

Jon
bug_id=6819886... which I just mentioned 3 minutes ago ;)
VonC
*sigh* if only I'd added my response 3 minutes prior, instead of searching for a complete list of values for the poor guy...
Jon
Thanks jon, appreciated. I had already seen that list, as you see the newer OSes are missing...
raticulin
+2  A: 

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
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}
raticulin
+1  A: 

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
we only support Sun jvm so this is not an issue for us, but I see you point...
raticulin
A: 

how to check for MAC OS..?

Kumar
You're posing a question, rather than answering. There's a special button for posing questions.
nes1983