11

I want to check Windows edition (Basic or Home or Professional or Business or other) in Java.

How do I do this?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
lupchiazoem
  • 8,026
  • 6
  • 36
  • 42
  • How would you check it outside Java? – Thorbjørn Ravn Andersen May 24 '11 at 11:34
  • This article provide some details about the Windows version and edition http://msdn.microsoft.com/en-us/library/ms724429(v=vs.85).aspx – Damian Leszczyński - Vash May 24 '11 at 11:53
  • @Vash, even though your article is helpful, the OP wants to get the info from Java (and not C/C++) – Buhake Sindi May 24 '11 at 11:59
  • @The Elite Gentleman, I realize that. The my intention was to put some light on the way how those data are really stored in system, because the edition is a numeric value not a prepared char set. – Damian Leszczyński - Vash May 24 '11 at 12:09
  • Everybody! Thanks much for your inputs. But, the hunt is still on - I'm looking to find out Windows "EDITION" as stated in OP. – lupchiazoem May 24 '11 at 14:54
  • Not sure if it's a suitable solution for you, but you could run a batch file from java which issues 'systeminfo > whatever.txt' and then you parse the .txt for OS Name which i think contains the edition. Or you call GetProductInfo via JNI. – abalogh May 24 '11 at 18:28
  • @Sannidhi: You can target the right windows version by using the [solution provided by CubaLibre](http://commons.apache.org/lang/api/org/apache/commons/lang3/SystemUtils.html#IS_OS_WINDOWS_2000) – Bart Vangeneugden May 25 '11 at 06:27

6 Answers6

8

You can always use Java to call the Windows command 'systeminfo' then parse out the result, I can't seem to find a way to do this natively in 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 McMillen
  • 59,865
  • 24
  • 119
  • 170
  • Hunter, you are right, not possible through Java directly and thanks for your response. Though I had a solution before your post, I didn't share for some personal reason. Actually, there's a better way: – lupchiazoem May 27 '11 at 01:41
  • 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 ) ... – lupchiazoem May 27 '11 at 01:47
6

You can use the Apache Commons Library

The class SystemUtils provides several methods to determine such information.

CubaLibre
  • 375
  • 1
  • 2
  • 14
4

You can get a lot of information about the System you're running on by asking the JVM about it's System Properties:

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

more info here: http://www.herongyang.com/Java/System-JVM-and-OS-System-Properties.html

EDIT: the property os.name seems to be your best bet

Bart Vangeneugden
  • 3,436
  • 4
  • 33
  • 52
  • I was thinking about this, but it doesn't return the exact info (windows type) needed by the OP. Maybe he can execute a windows command and read it from that `Process`. – asgs May 24 '11 at 11:40
3
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"));
}

output:

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

For more info(the most important system properties):

fitorec
  • 4,257
  • 2
  • 24
  • 18
1

The results from System.getProperty("os.name") vary between different Java virtual machines (even the Sun/Oracle ones):

A JRE will return Windows 8 for a windows 8 machine. For the same system a Windows NT (unknown) is returned when running the same program with a JDK.

System.getProperty("os.version") seems more reliable on this. For Windows 7 it returns 6.1 and 6.2 for Windows 8.

benka
  • 4,732
  • 35
  • 47
  • 58
0

Refactored Hunter McMillen's answer to be more efficient and 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;
    }
}
Community
  • 1
  • 1
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132