Script to Determine Java Version

Javatester.org lists several ways to check the installed version of Java. Many of those methods are not programmatic and the programmatic methods mostly involve doing something within a web browser.

 

Sun's Java installs using a Windows Installer (MSI) package. Therefore you can use Win32_Product to see information about installed packages. You can simply run wmic product at a command prompt to output the information on all installed MSI packages. There are plenty of other installer technologies, so running wmic product will only return a subset of the software installed on your machine, unless of course every application was installed as an MSI package, which is unlikely.

 

You can filter that WMIC command to return just the information about Java packages:

wmic product where "name LIKE '%java%'"  get * /format:textvaluelist

 

Sample output:

 

AssignmentType=1
Caption=Java(TM) 6 Update 10
Description=Java(TM) 6 Update 10
HelpLink=http://java.com
HelpTelephone=
IdentifyingNumber={26A24AE4-039D-4CA4-87B4-2F83216010FF}
InstallDate=20081126
InstallDate2=
InstallLocation=D:\Program Files (x86)\Java\jre6\
InstallSource=D:\Users\craig\AppData\LocalLow\Sun\Java\jre1.6.0_10\
InstallState=5
Language=0
LocalPackage=D:\Windows\Installer\4997911e.msi
Name=Java(TM) 6 Update 10
PackageCache=D:\Windows\Installer\4997911e.msi
PackageCode={AB372567-E9F7-4CF3-A28D-28EE632050A5}
PackageName=jre1.6.0_10-c-l.msi
ProductID=none
RegCompany=
RegOwner=Craig
SKUNumber=
Transforms=D:\Windows\Installer\26A24AE4-039D-4CA4-87B4-2F83216010FF}\sp1033.MST
URLInfoAbout=http://java.com
URLUpdateInfo=http://java.sun.com
Vendor=Sun Microsystems, Inc.
Version=6.0.100
WordCount=0

 

Information about those properties is on MSDN.

 

Note that the Windows Installer WMI Provider, which is what allows you to query the Win32_Product WMI class, is not installed by default on Windows Server 2003. Also, WMIC is not available on Windows 2000 nor is the LIKE operator if you were to try to script it instead of using WMIC.

 

Below is a script that tries to use Win32_Product first, using InStr to filter on any strings containing the word Java. If it returns an error indicating Win32_Product is unavailable, it then loops through the entire Uninstall key in the registry, again filtering on anything containing the string Java. Win32_Product provides more information than the registry method, so if it is available, the script tries to use it.

 

Since the script relies on WMI for the Windows Installer information and also the registry information, it would be simple enough to make remotable.

On Error  Resume Next
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_SZ = 1
blnJavaInstalled = False
dtmStart = Timer
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProducts = objWMIService.ExecQuery("SELECT Caption,Description,InstallDate,InstallLocation,InstallState,Name,Vendor,Version FROM Win32_Product")
For Each  objProduct in colProducts
    If Err.Number = 0 Then
     If InStr(UCase(objProduct.Name),"JAVA") Then
      Wscript.Echo "Caption=" & objProduct.Caption
      Wscript.Echo "Description="  & objProduct.Description
      Wscript.Echo "InstallDate="  & objProduct.InstallDate
      Wscript.Echo "InstallLocation="  & objProduct.InstallLocation
      Wscript.Echo "InstallState="  & objProduct.InstallState
      Wscript.Echo "Name=" & objProduct.Name
      Wscript.Echo "Vendor=" & objProduct.Vendor
      Wscript.Echo "Version=" & objProduct.Version & vbCrLf
      blnJavaInstalled = True
     End If
    Else
        hDefKey = HKEY_LOCAL_MACHINE
        strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Uninstall"
        Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
        objReg.EnumKey hDefKey, strKeyPath, arrSubKeys
        For Each  strSubkey In  arrSubKeys
         strSubKeyPath = strKeyPath & "\" & strSubkey
         objReg.EnumValues hDefKey, strSubKeyPath, arrValueNames, arrTypes
         For i = LBound(arrValueNames) To UBound(arrValueNames)
          strValueName = arrValueNames(i)
          If arrTypes(i) = REG_SZ Then
           objReg.GetStringValue hDefKey, strSubKeyPath, strValueName, strValue
           If InStr(UCase(strValue),"JAVA") Then
               Wscript.Echo strValueName & " = " & strValue
               blnJavaInstalled = True
              End If
          End If
         Next
        Next
    End If
Next
If blnJavaInstalled <> True Then
 Wscript.Echo "No versions of Java were found."
End If
Wscript.Echo "Script execution time: "  & Round(Timer - dtmStart, 2) & " seconds"