2011-05-24 234 views
10

我想用Java检查Windows版本(基本或家庭或专业或业务或其他)。如何在Java中检查Windows版本?

我该怎么做?

+0

你怎么会检查它的Java之外? – 2011-05-24 11:34:34

+0

本文提供有关Windows版本和版本的一些详细信息http://msdn.microsoft.com/en-us/library/ms724429(v=vs.85).aspx – 2011-05-24 11:53:42

+0

@Vash,即使您的文章很有帮助, OP想从Java获得信息(而不是C/C++) – 2011-05-24 11:59:52

回答

7

你总是可以使用Java来调用Windows命令'systeminfo',然后解析出结果,我似乎无法找到一种方法在Java中本地执行此操作。

JRE一个将返回Windows 8用于窗户8机:

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

+1。试图涵盖OP所需的唯一答案。 – khachik 2011-05-24 19:20:06

+0

Hunter,你是对的,不可能通过Java直接进行,谢谢你的回应。虽然我在你的帖子之前已经有了一个解决方案,但由于某些个人原因我没有分享。实际上,还有更好的方法: – San 2011-05-27 01:41:13

+1

Process process = Runtime.getRuntime()。exec(“CMD/C SYSTEMINFO | FINDSTR/B/C:\”OS Name \“”); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = bufferedReader.readLine(); if(line.indexOf(“Professional”)> 0) ... – San 2011-05-27 01:47:23

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似乎是您最好的选择

+0

我正在考虑这个问题,但它并没有返回OP所需的确切信息(窗口类型)。也许他可以执行一个Windows命令并从该'Process'中读取它。 – asgs 2011-05-24 11:40:56

5

可以使用Apache Commons Library

SystemUtils类提供了几种确定此类信息的方法。

+0

+1 @CubaLibre非常有用的东西,它似乎提供了OP想要的东西。 – Boro 2011-05-24 11:47:48

+2

我不确定OP是如何实现的,因为'SystemUtils''文件提到它们会返回JVM的标准属性'os.name','os.version'和'os.arch'。 – asgs 2011-05-24 11:51:19

1

System.getProperty("os.name")结果不同的Java虚拟机(连Sun/Oracle的那些)之间变化。对于同一个系统,在运行与JDK相同的程序时返回Windows NT (unknown)

System.getProperty("os.version")对此似乎更可靠。对于Windows 7,它返回6.16.2对于Windows 8

0

重构Hunter McMillen的answer更有效率和可扩展性。

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

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