Java 的 "os.name" 对于 Windows 10?
Java's "os.name" for Windows 10?
在Java中,我们可以看到os.name
的属性值就知道底层操作系统的名称:System.getProperty("os.name")
.
对于 Windows 的每个版本,它曾经 return 总是 OS 的确切名称:Windows XP
用于 XP,Windows Vista
用于 Vista , Windows 7
代表七,Windows 8.1
代表 8.1,依此类推...
问题是:我刚刚使用已发布的 Microsoft 更新程序将我的 Windows 8.1 更新为 Windows 10,但似乎 属性 仍然存在 Windows 8.1
:
public class OSTest {
public static void main(String[] args) {
System.out.println(System.getProperty("os.name"));
}
}
我该如何解决这个问题?而且,有谁知道如果安装新的 Windows 10 副本,这个问题是否仍然存在 - 也就是说,这个错误是由 Microsoft 自动更新程序引起的 -?
这是一个已知问题 JDK-8066504,已在即将发布的 Java 8 更新 60 中修复。
原因是 GetVersionEx 函数自 Windows 8.1 以来改变了它的行为。
有多种可能的解决方法,请参阅 MSDN article。
最简单的是执行 cmd.exe /c ver
.
另一种是查看其中一个系统文件的版本信息,例如kernel32.dll
.
这绝对是一个已知错误。这是因为 os.name
属性 从 Windows API 的源代码中的 GetVersionEx
获取其值。 GetVersionEx
然而,
may be altered or unavailable for releases after Windows 8.1
根据微软官方网站。相反,我们需要使用 versionhelpers.h
文件中版本助手 API 函数中的 IsWindows10OrGreater
。正如您可能猜到的那样,这个文件不是 Java 文件,它是用 C 语言编写的。因此我们需要以一种有点迂回的方式包含它。它确实需要相当多的工作(您需要在 JNI 中编程 :/)但是 this tutorial will help you do it. Another solution is shown in this bug log,并且确实需要更少的工作。
您也可以使用 .contains()
方法并只检查 "windows"
字符串可能符合
if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains(windows version here [xp, 7, 8, etc]))){}
如果您需要 windows 版本,您可以检查所有版本,然后假定为 8.1 或 10 以解决该错误。
if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("xp")){
//code for windows xp }
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("vista")){
//code for windows vista
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("7")){
//code for windows 7}
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("8")){
//code for windows 8}
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("8.1")){
//code for both windows 8.1 and 10
}
现在解释一下这里发生了什么:
if语句只是判断windows
版本的条件
System.getProperty("os.name")
returns的名字os为字符串
.toLowerCase()
方法使返回的字符串小写
.contains(String)
方法检查给定的输入字符串是否包含在调用它的字符串中
最后一条语句允许每个 os 使用不同的代码,除了 8.1 和 10 需要作为一个块处理:(
嗯...我不知道它是 Windows 10(10.0.17134.590
) 还是 Java 8(1.8.0_171-b11
对我来说) ,但现在是正确的:os.name
给我 Windows 10
.
另外,不信任的可以查看os.version
。我有 10.0
。
(顺便说一下,我看到 os.arch:amd64
。这是 JVM 的,而不是 OS。)
我遇到了同样的问题,使用了以下解决方法:
cmd 命令 "systeminfo" returns "OS Name:" 这是 OS 的正确名称,为此编写了以下函数:
private boolean os2k10Check()
{
try{
Process p = Runtime.getRuntime().exec("systeminfo"); /*Execute cmd command "systeminfo"*/
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true)
{
line = r.readLine();
if (line == null) { break; }
if(line.contains("OS Name:")) /*If output contains OS Name and 2010*/
{
if(line.contains("2010"))
return true;
else
return false;
}
}
}
catch(Exception e)
{System.out.println("Platform Type: os2010check: exception"+e);}
return false;
}
在Java中,我们可以看到os.name
的属性值就知道底层操作系统的名称:System.getProperty("os.name")
.
对于 Windows 的每个版本,它曾经 return 总是 OS 的确切名称:Windows XP
用于 XP,Windows Vista
用于 Vista , Windows 7
代表七,Windows 8.1
代表 8.1,依此类推...
问题是:我刚刚使用已发布的 Microsoft 更新程序将我的 Windows 8.1 更新为 Windows 10,但似乎 属性 仍然存在 Windows 8.1
:
public class OSTest {
public static void main(String[] args) {
System.out.println(System.getProperty("os.name"));
}
}
我该如何解决这个问题?而且,有谁知道如果安装新的 Windows 10 副本,这个问题是否仍然存在 - 也就是说,这个错误是由 Microsoft 自动更新程序引起的 -?
这是一个已知问题 JDK-8066504,已在即将发布的 Java 8 更新 60 中修复。
原因是 GetVersionEx 函数自 Windows 8.1 以来改变了它的行为。
有多种可能的解决方法,请参阅 MSDN article。
最简单的是执行 cmd.exe /c ver
.
另一种是查看其中一个系统文件的版本信息,例如kernel32.dll
.
这绝对是一个已知错误。这是因为 os.name
属性 从 Windows API 的源代码中的 GetVersionEx
获取其值。 GetVersionEx
然而,
may be altered or unavailable for releases after Windows 8.1
根据微软官方网站。相反,我们需要使用 versionhelpers.h
文件中版本助手 API 函数中的 IsWindows10OrGreater
。正如您可能猜到的那样,这个文件不是 Java 文件,它是用 C 语言编写的。因此我们需要以一种有点迂回的方式包含它。它确实需要相当多的工作(您需要在 JNI 中编程 :/)但是 this tutorial will help you do it. Another solution is shown in this bug log,并且确实需要更少的工作。
您也可以使用 .contains()
方法并只检查 "windows"
字符串可能符合
if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains(windows version here [xp, 7, 8, etc]))){}
如果您需要 windows 版本,您可以检查所有版本,然后假定为 8.1 或 10 以解决该错误。
if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("xp")){
//code for windows xp }
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("vista")){
//code for windows vista
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("7")){
//code for windows 7}
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("8")){
//code for windows 8}
else if (System.getProperty("os.name").toLowerCase().contains("windows") && System.getProperty("os.name").toLowerCase().contains("8.1")){
//code for both windows 8.1 and 10
}
现在解释一下这里发生了什么:
if语句只是判断windows
版本的条件
System.getProperty("os.name")
returns的名字os为字符串.toLowerCase()
方法使返回的字符串小写.contains(String)
方法检查给定的输入字符串是否包含在调用它的字符串中最后一条语句允许每个 os 使用不同的代码,除了 8.1 和 10 需要作为一个块处理:(
嗯...我不知道它是 Windows 10(10.0.17134.590
) 还是 Java 8(1.8.0_171-b11
对我来说) ,但现在是正确的:os.name
给我 Windows 10
.
另外,不信任的可以查看os.version
。我有 10.0
。
(顺便说一下,我看到 os.arch:amd64
。这是 JVM 的,而不是 OS。)
我遇到了同样的问题,使用了以下解决方法: cmd 命令 "systeminfo" returns "OS Name:" 这是 OS 的正确名称,为此编写了以下函数:
private boolean os2k10Check()
{
try{
Process p = Runtime.getRuntime().exec("systeminfo"); /*Execute cmd command "systeminfo"*/
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true)
{
line = r.readLine();
if (line == null) { break; }
if(line.contains("OS Name:")) /*If output contains OS Name and 2010*/
{
if(line.contains("2010"))
return true;
else
return false;
}
}
}
catch(Exception e)
{System.out.println("Platform Type: os2010check: exception"+e);}
return false;
}