Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中检查Windows版本?_Java_Windows_Windowsversion - Fatal编程技术网

如何在Java中检查Windows版本?

如何在Java中检查Windows版本?,java,windows,windowsversion,Java,Windows,Windowsversion,我想用Java检查Windows版本(基本版、家庭版、专业版、商务版或其他版) 如何做到这一点?通过询问JVM的系统属性,您可以获得有关正在运行的系统的大量信息: import java.util.*; public class SysProperties { public static void main(String[] a) { Properties sysProps = System.getProperties(); sysProps.list(System

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


如何做到这一点?

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

import java.util.*;
public class SysProperties {
   public static void main(String[] a) {
      Properties sysProps = System.getProperties();
      sysProps.list(System.out);
   }
}
更多信息请点击此处:

编辑:属性
os.name
似乎是您的最佳选择

您可以使用


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

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

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

对于Windows 8计算机,
JRE
将返回
Windows 8
。对于同一系统,当使用
JDK
运行同一程序时,将返回
windowsnt(未知)


System.getProperty(“os.version”)
在这方面似乎更可靠。对于
windows7
,它返回
6.1
6.2
,对于
windows8
,重构后的Hunter-McMillen's将更加高效和可扩展

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;
    }
}
输出:

os.name: Windows 8.1
os.version: 6.3
os.arch: amd64
有关更多信息(最重要的系统属性):


您将如何在Java之外检查它?本文提供了有关Windows版本和版本@Vash的一些详细信息,尽管您的文章很有帮助,OP希望从Java(而不是C/C++)获取信息@Elite Gentle,我意识到这一点。我的目的是让大家了解这些数据在系统中的实际存储方式,因为版本是一个数值,而不是一个准备好的字符集!非常感谢您的投入。但是,搜索仍在进行中-我正在查找OP中所述的Windows“EDITION”。我考虑过这一点,但它没有返回OP所需的确切信息(Windows类型)。也许他可以执行Windows命令,并从
进程中读取
+1@CubaLibre非常有用的内容,而且它似乎提供了OP想要的东西。我不确定OP想要的是什么,因为
SystemUtils
“文档提到它们将返回JVM的标准属性
os.name
os.version
,和
os.arch
。链接是404。Hunter,你说得对,不可能直接通过Java实现,谢谢您的回复。虽然在你发帖之前我有一个解决方案,但出于个人原因我没有分享。实际上,有一种更好的方法:Process Process=Runtime.getRuntime().exec(“CMD/C SYSTEMINFO | FINDSTR/B/C:\'OS Name\”);BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(process.getInputStream());String line=bufferedReader.readLine();如果(line.indexOf(“Professional”)>0)…更新:对于windows 10和jre11,显然os.name根本不可靠。
os.name: Windows 8.1
os.version: 6.3
os.arch: amd64