我想在Java中检查Windows版本(基础版、家庭版、专业版、商业版或其他)。
我该怎么做呢?
发布于 2011-05-25 02:19:29
你总是可以使用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());
}
}
}发布于 2011-05-24 19:41:32
您可以使用Apache Commons Library
SystemUtils类提供了几种方法来确定此类信息。
发布于 2011-05-24 19:39:29
通过询问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似乎是你最好的选择
https://stackoverflow.com/questions/6109679
复制相似问题