如何用Java检查Windows版本?

lup*_*oem 10 java windows windowsversion

我想用Java检查Windows版本(基本版或家庭版或专业版或商业版或其他版).

我该怎么做呢?

Hun*_*len 7

您总是可以使用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());
            }
      }
   }
Run Code Online (Sandbox Code Playgroud)


Cub*_*bre 5

您可以使用Apache Commons Library

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

  • 我不确定OP是怎么想的,因为`SystemUtils`'文档提到他们将返回JVM的标准属性`os.name`,`os.version`和`os.arch`. (2认同)