如何在Java中检查Windows版本?
我想用Java检查Windows版本(基本版、家庭版、专业版、商务版或其他版)如何在Java中检查Windows版本?,java,windows,windowsversion,Java,Windows,Windowsversion,我想用Java检查Windows版本(基本版、家庭版、专业版、商务版或其他版) 如何做到这一点?通过询问JVM的系统属性,您可以获得有关正在运行的系统的大量信息: import java.util.*; public class SysProperties { public static void main(String[] a) { Properties sysProps = System.getProperties(); sysProps.list(System
如何做到这一点?通过询问JVM的系统属性,您可以获得有关正在运行的系统的大量信息:
import java.util.*;
public class SysProperties {
public static void main(String[] a) {
Properties sysProps = System.getProperties();
sysProps.list(System.out);
}
}
更多信息请点击此处:
编辑:属性os.name
似乎是您的最佳选择您可以使用
SystemUtils类提供了几种确定此类信息的方法。您始终可以使用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());
}
}
}
System.getProperty(“os.name”)
的结果在不同的Java虚拟机(甚至Sun/Oracle虚拟机)之间有所不同:
对于Windows 8计算机,JRE
将返回Windows 8
。对于同一系统,当使用JDK
运行同一程序时,将返回windowsnt(未知)
System.getProperty(“os.version”)
在这方面似乎更可靠。对于windows7
,它返回6.1
和6.2
,对于windows8
,重构后的Hunter-McMillen's将更加高效和可扩展
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;
}
}
输出:
os.name: Windows 8.1
os.version: 6.3
os.arch: amd64
有关更多信息(最重要的系统属性):
进程中读取+1@CubaLibre非常有用的内容,而且它似乎提供了OP想要的东西。我不确定OP想要的是什么,因为SystemUtils
“文档提到它们将返回JVM的标准属性os.name
,os.version
,和os.arch
。链接是404。Hunter,你说得对,不可能直接通过Java实现,谢谢您的回复。虽然在你发帖之前我有一个解决方案,但出于个人原因我没有分享。实际上,有一种更好的方法:Process Process=Runtime.getRuntime().exec(“CMD/C SYSTEMINFO | FINDSTR/B/C:\'OS Name\”);BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(process.getInputStream());String line=bufferedReader.readLine();如果(line.indexOf(“Professional”)>0)…更新:对于windows 10和jre11,显然os.name根本不可靠。
os.name: Windows 8.1
os.version: 6.3
os.arch: amd64