我想用Java检查Windows版本(基本版或家庭版或专业版或商业版或其他版)。
我该怎么做?
答案 0 :(得分:7)
您总是可以使用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());
}
}
}
答案 1 :(得分:5)
SystemUtils类提供了几种确定此类信息的方法。
答案 2 :(得分:4)
通过向JVM询问系统属性,您可以获得有关正在运行的系统的大量信息:
import java.util.*;
public class SysProperties {
public static void main(String[] a) {
Properties sysProps = System.getProperties();
sysProps.list(System.out);
}
}
此处有更多信息:http://www.herongyang.com/Java/System-JVM-and-OS-System-Properties.html
编辑:属性os.name
似乎是您最好的选择
答案 3 :(得分:1)
System.getProperty("os.name")
的结果因不同的Java虚拟机(甚至是Sun / Oracle的虚拟机)而异:
对于Windows 8计算机,JRE
将返回Windows 8
。对于同一系统,使用Windows NT (unknown)
运行相同的程序时会返回JDK
。
System.getProperty("os.version")
似乎更可靠。对于Windows 7
,它会为6.1
返回6.2
和Windows 8
。
答案 4 :(得分:0)
让Hunter McMillen的this link更加高效和可扩展。
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;
}
}
答案 5 :(得分:0)
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"));
}
输出:
os.name: Windows 8.1
os.version: 6.3
os.arch: amd64
有关详细信息(最重要的系统属性):