如何在 Java 中检查 Windows 版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6109679/
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
How to check Windows edition in Java?
提问by lupchiazoem
I want to check Windows edition (Basic or Home or Professional or Business or other) in Java.
我想在 Java 中检查 Windows 版本(Basic 或 Home 或 Professional 或 Business 或其他)。
How do I do this?
我该怎么做呢?
采纳答案by Hunter McMillen
You can always use Java to call the Windows command 'systeminfo' then parse out the result, I can't seem to find a way to do this natively in Java.
您始终可以使用 Java 调用 Windows 命令“systeminfo”,然后解析结果,我似乎找不到在 Java 中本地执行此操作的方法。
import java.io.*;
public class GetWindowsEditionTest
{
public static void main(String[] args)
{
Runtime rt;
Process pr;
BufferedReader in;
String line = "";
String sysInfo = "";
String edition = "";
String fullOSName = "";
final String SEARCH_TERM = "OS Name:";
final String[] EDITIONS = { "Basic", "Home",
"Professional", "Enterprise" };
try
{
rt = Runtime.getRuntime();
pr = rt.exec("SYSTEMINFO");
in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
//add all the lines into a variable
while((line=in.readLine()) != null)
{
if(line.contains(SEARCH_TERM)) //found the OS you are using
{
//extract the full os name
fullOSName = line.substring(line.lastIndexOf(SEARCH_TERM)
+ SEARCH_TERM.length(), line.length()-1);
break;
}
}
//extract the edition of windows you are using
for(String s : EDITIONS)
{
if(fullOSName.trim().contains(s))
{
edition = s;
}
}
System.out.println("The edition of Windows you are using is "
+ edition);
}
catch(IOException ioe)
{
System.err.println(ioe.getMessage());
}
}
}
回答by CubaLibre
You can use the Apache Commons Library
您可以使用Apache Commons 库
The class SystemUtils provides several methods to determine such information.
SystemUtils 类提供了多种方法来确定此类信息。
回答by Bart Vangeneugden
You can get a lot of information about the System you're running on by asking the JVM about it's System Properties:
通过向 JVM 询问系统属性,您可以获得有关正在运行的系统的大量信息:
import java.util.*;
public class SysProperties {
public static void main(String[] a) {
Properties sysProps = System.getProperties();
sysProps.list(System.out);
}
}
more info here: http://www.herongyang.com/Java/System-JVM-and-OS-System-Properties.html
更多信息:http: //www.herongyang.com/Java/System-JVM-and-OS-System-Properties.html
EDIT:the property os.name
seems to be your best bet
编辑:该物业os.name
似乎是您最好的选择
回答by user3804568
The results from System.getProperty("os.name")
vary between different Java virtual machines (even the Sun/Oracle ones):
System.getProperty("os.name")
不同 Java 虚拟机(甚至是 Sun/Oracle 虚拟机)的结果不同:
A JRE
will return Windows 8
for a windows 8 machine. For the same system a Windows NT (unknown)
is returned when running the same program with a JDK
.
AJRE
将返回Windows 8
Windows 8 机器。对于相同的系统,Windows NT (unknown)
当运行带有JDK
.
System.getProperty("os.version")
seems more reliable on this. For Windows 7
it returns 6.1
and 6.2
for Windows 8
.
System.getProperty("os.version")
在这方面似乎更可靠。因为Windows 7
它返回6.1
并且6.2
对于Windows 8
.
回答by fitorec
public static void main(String[] args) {
System.out.println("os.name: " + System.getProperty("os.name"));
System.out.println("os.version: " + System.getProperty("os.version"));
System.out.println("os.arch: " + System.getProperty("os.arch"));
}
output:
输出:
os.name: Windows 8.1
os.version: 6.3
os.arch: amd64
For more info(the most important system properties):
有关更多信息(最重要的系统属性):
回答by Mr. Polywhirl
Refactored Hunter McMillen's answerto be more efficient and extensible.
重构 Hunter McMillen 的答案以提高效率和可扩展性。
import java.io.*;
public class WindowsUtils {
private static final String[] EDITIONS = {
"Basic", "Home", "Professional", "Enterprise"
};
public static void main(String[] args) {
System.out.printf("The edition of Windows you are using is: %s%n", getEdition());
}
public static String findSysInfo(String term) {
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("CMD /C SYSTEMINFO | FINDSTR /B /C:\"" + term + "\"");
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
return in.readLine();
} catch (IOException e) {
System.err.println(e.getMessage());
}
return "";
}
public static String getEdition() {
String osName = findSysInfo("OS Name:");
if (!osName.isEmpty()) {
for (String edition : EDITIONS) {
if (osName.contains(edition)) {
return edition;
}
}
}
return null;
}
}