10

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

我该怎么做呢?

4

6 回答 6

8

您始终可以使用 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-24T18:19:28.873 回答
5

您可以使用Apache Commons Library

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

于 2011-05-24T11:41:31.890 回答
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似乎是您最好的选择

于 2011-05-24T11:39:29.493 回答
2
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

有关更多信息(最重要的系统属性):

于 2017-08-10T19:40:31.487 回答
1

System.getProperty("os.name")不同 Java 虚拟机(甚至是 Sun/Oracle 虚拟机)的结果各不相同:

AJRE将返回Windows 8Windows 8 机器。对于同一个系统,使用 aWindows NT (unknown)运行同一个程序时会返回 a JDK

System.getProperty("os.version")在这方面似乎更可靠。因为Windows 7它返回6.16.2for Windows 8

于 2014-07-04T07:57:21.913 回答
0

重构 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;
    }
}
于 2015-09-04T10:50:34.040 回答