如何在Java中检查Windows版本?

我想在Java中检查Windows版本(Basic或Home或Professional或Business或其他)。

我该怎么做呢?

Solutions Collecting From Web of "如何在Java中检查Windows版本?"

你总是可以使用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的)之间有所不同:

JRE将返回Windows 8 8机器的Windows 8。 对于同一个系统,在与JDK运行相同的程序时返回一个Windows NT (unknown)

System.getProperty("os.version")在这个上似乎更可靠。 对于Windows 7Windows 8将返回6.16.2

重构亨特麦克米伦的答案是更有效率和可扩展性。

 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; } } 
 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 

欲了解更多信息(最重要的系统属性):