【问题标题】:How to check Windows edition in Java?如何在 Java 中检查 Windows 版本?
【发布时间】:2011-08-31 20:49:31
【问题描述】:

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

我该怎么做?

【问题讨论】:

  • 如何在 Java 之外检查它?
  • 这篇文章提供了一些关于Windows版本和版本msdn.microsoft.com/en-us/library/ms724429(v=vs.85).aspx的详细信息
  • @Vash,即使您的文章很有帮助,OP 也希望从 Java(而不是 C/C++)获取信息
  • @精英绅士,我意识到这一点。我的目的是阐明这些数据是如何真正存储在系统中的,因为版本是一个数值而不是一个准备好的字符集。
  • 大家好!非常感谢您的投入。但是,搜索仍在进行中 - 我正在寻找 OP 中所述的 Windows“版本”。

标签: java windows windowsversion


【解决方案1】:

您始终可以使用 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());
            }
      }
   }

【讨论】:

  • Hunter,您说得对,直接通过 Java 是不可能的,感谢您的回复。虽然我在你的帖子之前有一个解决方案,但出于某种个人原因我没有分享。其实还有更好的办法:
  • 进程进程 = 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("专业") > 0) ...
【解决方案2】:

您可以使用Apache Commons Library

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

【讨论】:

  • +1 @CubaLibre 非常有用的东西,它似乎提供了 OP 想要的东西。
  • 我不确定 OP 想要什么,因为 SystemUtils' 文档提到它们将返回 JVM 的标准属性 os.nameos.versionos.arch。跨度>
  • 链接是404。
【解决方案3】:

您可以通过询问 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 似乎是你最好的选择

【讨论】:

  • 我在考虑这个,但它没有返回 OP 所需的确切信息(Windows 类型)。也许他可以执行一个windows命令并从Process读取它。
【解决方案4】:
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

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

【讨论】:

    【解决方案5】:

    System.getProperty("os.name") 的结果在不同的 Java 虚拟机(甚至是 Sun/Oracle 虚拟机)之间有所不同:

    对于 Windows 8 机器,JRE 将返回 Windows 8。对于同一系统,当使用 JDK 运行同一程序时,将返回 Windows NT (unknown)

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

    【讨论】:

    • 更新:windows 10 和 jre11 显然 os.name 根本不可靠。
    【解决方案6】:

    重构了 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-12
      • 2017-12-01
      • 2010-12-30
      • 2013-07-21
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2020-10-16
      相关资源
      最近更新 更多