本文介绍了如何用Java检查Windows版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用Java检查Windows版本(基本版或家庭版或专业版或商业版或其他版)。
I want to check Windows edition (Basic or Home or Professional or Business or other) in Java.
我该怎么做?
推荐答案
你总是可以用Java来调用Windows命令'systeminfo'然后解析出结果,我似乎找不到办法做到这一点原生于Java。
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.
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());
}
}
}
这篇关于如何用Java检查Windows版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!