Recently I moved on a brand new machine 64-bit Windows 7. But when I run this code, getting the incorrect OS name
String osName = System.getProperty("os.name");
System.out.println("OS Name = " + osName);
The output comes:
OS Name = Windows Vista
Any idea, what is wrong in my code or system?
Thanks in Advance.
You might be using an older version of Java. As this was a known bug(bug_id=6819886) that has been fixed in the newer versions. Kindly read this for further details .
A possible workaround for this in case you are not able to upgrade your java version:
String osName = System.getProperty("os.name");
if (osName.equals("Windows XP") || osName.equals("Windows Vista"))
{
//do something and remember to put in all the names in the above if list. I just added two for example,it will have to include all like Windows NT,ME,95,etc.
}
else
{
//the block that will be accessible for Windows 7
}
There is a bug reported regarding this:
http://bugs.sun.com/view_bug.do?bug_id=6819886
Not sure if it is fixed in newer versions of Java as I do not have Java 7.
It works on my Windows 7 machine (admittedly a 32-bit one; I don't have access to a 64-bit one right now).
Perhaps your JRE predates Windows 7, and the name is baked into it? Which version of the JRE are you using? I would suggest updating to the latest version and trying again. Admittedly it's pretty nasty if the JRE does have the OS names hard-coded into it, but stranger things have happened.
使用JAVA-6,我试过它工作正常,否则你的Windows正在使用Vista模式处理JVM。
Ran into the same problem recently. As the bug 6819886 evaluation notes state, you can check the os.version property to distinguish between Windows 7 and Windows Vista in this case.
The version for Windows 7 is 6.1 and that for Windows Vista is 6.
String osVersion = System.getProperty("os.version");
if("6.1".equals(osVersion)){
System.out.println("OS is Windows 7");
}
This way you don't have to upgrade to the latest Java just to make this work.
String WinVer = System.getProperty("os.name");
if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("95")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("98")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("ME")){ System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("3.51")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("NT 4.0")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("2000")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("XP")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("7")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("8")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("8.1")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("10")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("2003")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("Vista")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("2008")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("2008 R2")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("2012")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("2012 R2")){System.out.println(WinVer);
} else if (WinVer.toLowerCase().contains("windows") && WinVer.toLowerCase().contains("2016")){System.out.println(WinVer);
}
Win 7 Test : Windows 7
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.