2011-05-24 4 views
10

Quiero verificar la edición de Windows (Basic o Home o Professional o Business u otra) en Java.¿Cómo verificar la edición de Windows en Java?

¿Cómo puedo hacer esto?

+0

Cómo habría que verlo fuera de Java? –

+0

Este artículo proporciona algunos detalles sobre la versión de Windows y la edición http://msdn.microsoft.com/en-us/library/ms724429(v=vs.85).aspx –

+0

@Vash, aunque su artículo es útil, el OP quiere obtener la información de Java (y no C/C++) –

Respuesta

7

Siempre puede usar Java para llamar al comando de Windows 'systeminfo' y luego analizar el resultado, parece que no puedo encontrar una manera de hacerlo de forma nativa en 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()); 
      } 
     } 
    } 
+1

+1. La única respuesta que trata de cubrir lo que quiere el OP. – khachik

+0

Hunter, tienes razón, no es posible a través de Java directamente y gracias por tu respuesta. Aunque tenía una solución antes de su publicación, no la compartí por algún motivo personal. En realidad, hay una mejor manera: – San

+1

Process process = 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 ("Professional")> 0) ... – San

4

Usted puede obtener una gran cantidad de información sobre el sistema se está ejecutando en preguntando a la JVM se registran para la Propiedades del sistema:

import java.util.*; 
public class SysProperties { 
    public static void main(String[] a) { 
     Properties sysProps = System.getProperties(); 
     sysProps.list(System.out); 
    } 
} 

información más aquí: http://www.herongyang.com/Java/System-JVM-and-OS-System-Properties.html

EDIT: la propiedad os.name parece ser la mejor opción

+0

Estaba pensando en esto, pero no devuelve la información exacta (tipo de Windows) que necesita el OP. Tal vez él puede ejecutar un comando de Windows y leerlo desde ese 'Proceso'. – asgs

5

puede utilizar el Apache Commons Library

La clase SystemUtils proporciona varios métodos para determinar dicha información.

+0

+1 @CubaLibre cosas muy útiles, y parece entregar lo que el OP quiere. – Boro

+2

No estoy seguro de cómo esto es lo que OP quiere porque los documentos de 'SystemUtils' mencionan que devolverán las propiedades estándar de la JVM' os.name', 'os.version' y' os.arch'. – asgs

1

Los resultados de System.getProperty("os.name") varían entre diferentes máquinas virtuales Java (incluso los de Sun/Oracle):

Un JREWindows 8 volverá para una máquina de Windows 8. Para el mismo sistema, se devuelve un Windows NT (unknown) cuando se ejecuta el mismo programa con un JDK.

System.getProperty("os.version") parece más confiable en esto. Para Windows 7 devuelve 6.1 y 6.2 para Windows 8.

0

Refactorizado Hunter McMillen answer para ser más eficiente y extensible.

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; 
    } 
} 
0
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")); 
} 

de salida:

os.name: Windows 8.1 
os.version: 6.3 
os.arch: amd64 

Para obtener más información (las propiedades más importantes del sistema):

Cuestiones relacionadas