首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Java中检查Windows版本?

如何在Java中检查Windows版本?
EN

Stack Overflow用户
提问于 2011-05-24 19:28:02
回答 6查看 10.9K关注 0票数 10

我想在Java中检查Windows版本(基础版、家庭版、专业版、商业版或其他)。

我该怎么做呢?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-05-25 02:19:29

你总是可以使用Java调用Windows命令'systeminfo‘,然后解析出结果,我似乎找不到一种在Java中本机实现这一点的方法。

代码语言:javascript
复制
 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());
            }
      }
   }
票数 8
EN

Stack Overflow用户

发布于 2011-05-24 19:41:32

您可以使用Apache Commons Library

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

票数 5
EN

Stack Overflow用户

发布于 2011-05-24 19:39:29

通过询问JVM的系统属性,您可以获得有关正在运行的系统的大量信息:

代码语言:javascript
复制
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似乎是你最好的选择

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6109679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档