Windows のエディション (Basic または Home または Professional または Business またはその他) を Java で確認したい。
どうすればいいですか?
Windows のエディション (Basic または Home または Professional または Business またはその他) を 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());
}
}
}
Apache Commons Libraryを使用できます
クラス SystemUtils は、そのような情報を判別するためのいくつかのメソッドを提供します。
システム プロパティについて 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
はあなたの最善の策のようです
の結果System.getProperty("os.name")
は、異なる Java 仮想マシン (Sun/Oracle の仮想マシンであっても) によって異なります。
Windows 8 マシンの場合はAJRE
が返されます。Windows 8
同じシステムの場合、aWindows NT (unknown)
を使用して同じプログラムを実行すると、a が返されJDK
ます。
System.getProperty("os.version")
これについてはより信頼できるようです。Windows 7
それが返されるため、6.1
および.6.2
Windows 8
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;
}
}