10

Windows のエディション (Basic または Home または Professional または Business またはその他) を Java で確認したい。

どうすればいいですか?

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 に答える
1

の結果System.getProperty("os.name")は、異なる Java 仮想マシン (Sun/Oracle の仮想マシンであっても) によって異なります。

Windows 8 マシンの場合はAJREが返されます。Windows 8同じシステムの場合、aWindows NT (unknown)を使用して同じプログラムを実行すると、a が返されJDKます。

System.getProperty("os.version")これについてはより信頼できるようです。Windows 7それが返されるため、6.1および.6.2Windows 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 に答える