Java System.getProperty("os.name") 在最新的 Windows 操作系统中返回什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2357758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what does System.getProperty("os.name") return in latest Windows OSs
提问by Persimmonium
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()...
我的一些代码在 x64 中失败了,我开始挖掘,这是由于一些代码通过 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.
我想为所有新操作系统(w2008,windows 7,...)修复此问题,但我无法访问每种类型的主机,而且我不想安装在 VM 中只是为了查看值,有人知道某处的一些清单吗?还没有找到。
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.
编辑:我需要:windows 7、windows 2003、windows 2008、windows 2008R2 另外,我不是 1.6u18,所以不用担心有些人提到的错误。
采纳答案by erikkallen
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;
}
回答by VonC
No list, but on Windows7, with a JDK6_u18:
没有列表,但在 Windows7 上,带有 JDK6_u18:
os.name
= "Windows 7"
os.name
= "Windows 7"
Note: there was a bug on JFK6_u14 and before, where it displayed:
注意:在 JFK6_u14 和之前有一个错误,它显示:
"Windows Vista" instead of "Windows 7" (even though the OS was actually "Windows 7"), so be careful!
“Windows Vista”而不是“Windows 7”(即使操作系统实际上是“Windows 7”),所以要小心!
According to this HowTo, it should be "Windows 2003" for W2003.
根据这个 HowTo,W2003 应该是“Windows 2003”。
回答by Fabian Steeg
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:
由于较新的版本应该需要 NT 行所需的内容,因此检查旧版本并使用 NT 设置而不是检查较新版本实际上可能更有意义,如下所示:
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;
}
回答by Jon
Depends on the version of Java that you're running, I came across this bug:
取决于您运行的 Java 版本,我遇到了这个错误:
http://bugs.sun.com/view_bug.do?bug_id=6819886
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 7just fine.
因此,只要您使用较新版本的 JDK,它就应该可以正常返回Windows 7。
Not sure about Windows Server 2008 though, I'm guessing Windows Server 2008.
虽然不确定 Windows Server 2008,但我猜是Windows Server 2008。
There's a reasonably complete list here:
这里有一个相当完整的列表:
回答by TofuBeer
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.
当 Visual Cafe 还活着的时候,我在赛门铁克处理过这个问题……我完全不建议这样做。问题是不同的供应商可以提供不同的字符串。我建议使用特定于操作系统的方式来确定平台。
You could use the "ver" utility on Windows and "uname" on Unix type systems.
您可以在 Windows 上使用“ver”实用程序,在 Unix 类型系统上使用“uname”。
It might be better to use "GetNativeSystemInfo" on Windows, but that would require native code.
在 Windows 上使用“GetNativeSystemInfo”可能会更好,但这需要本机代码。
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.
我建议采用这种方式而不是依赖 System.getProperty 方法的原因是因为您只需要处理底层操作系统而不是位于操作系统之上的 JVM - 这消除了不同虚拟机报告不同内容的问题同一个平台。
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.
编辑:显然您必须尝试不同的方式来获取信息,因为其中一些可能需要运行 shell 而不仅仅是命令。但是如果你坚持使用 bash 应该是好的。基本上尝试运行命令,直到其中一个工作......不漂亮但它会工作。
回答by Bass
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.
虽然这不是一个完整的解决方案,但您可以获得 32 位 JDK 并运行简单的代码打印os.name
和os.version
不同的兼容性设置。
Here're the os.name
/os.version
values reported by different JDKs on a Windows 8.1box:
以下是Windows 8.1 机器上不同 JDK 报告的os.name
/os.version
值:
╔═════════════════╤════════════╤════════════╤════════════╤═══════════════╤═══════════════╤══════════════════════╤══════════════════════╗ ║ 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 ║ ╚═════════════════╧════════════╧════════════╧════════════╧═══════════════╧═══════════════╧══════════════════════╧══════════════════════╝
回答by parag borah
This code will give you the latest windows OS name like "windows server 2016"
此代码将为您提供最新的 Windows 操作系统名称,例如“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;
}