Share

I was working on another login script for one of our clients a while back and ran into a situation where I needed to programmatically check if Java ™ was installed.  I then had another scenario where I needed to get the path to the JRE.  Unfortunately, I didn’t know any good way to do that at the time so I put together a couple of helper functions.  I’m not entirely sure if this is the best way to do this, but its the best method I’ve come up with so far, and has served me well for a few weeks now.

Some background:

The client is a school with a large number of student and staff workstations (whole labs, classrooms, etc).  They have a (rather poorly written IMHO) educational software package written in Java deployed all over the place.  As such, the primary requirement for this software is the Java Runtime Environment (JRE).  Unfortunately, not all the workstations have the JRE (intentionally in some cases).  The client requested that the script logic conditionally create shortcuts on the desktop for various URLs and applications, with the Java application being one of them.  Naturally, a condition for creating the shortcut to the Java application needed to be that Java actually be installed in the first place (the second being that the application itself is installed, and the other being that the user belonged to a particular security group).

But, the shortcut can’t just point to the .JAR file.  It has to launch the runtime with the .JAR as an argument (also could’ve created a batch file or other script to do the same thing, but why bother) which means I needed to know the path to the JRE so I could build the path to the javaw.exe binary.

The code:

The method I chose involves looking in the local machine’s registry.  So before I go too deep into the Java stuff, we’re going to need the following function.  The purpose of this function is to check for the existence of a named value within a specified key in a specified hive in the local registry:

Function RegValueExists(ByVal Hive, ByVal Key, ByVal Value)
   Const sComputer = "."
   Dim arrValNames
   Dim arrValTypes
   Dim idx
   Dim iRet
   Dim objReg
   iRet = False
 
   Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\default:StdRegProv")
   If (IsObject(objReg)) Then
      If (objReg.EnumValues(Hive, Key, arrValNames, arrValTypes) = 0) Then
         If (IsArray(arrValNames)) Then
            For idx = 1 To UBound(arrValNames)
               If (LCase(arrValNames(idx)) = LCase(Value)) Then
                  iRet = True
                  Exit For
               End If
            Next
         End If
      End If
   End If
 
   Set objReg = Nothing
   Erase arrValNames
   Erase arrValTypes
   RegValueExists = iRet
End Function

Now on to the good stuff. The following function will get the path to the newest version of installed JRE on the local machine:

Function GetJavaPath()
   On Error Resume Next
   Const HKLM = &H80000002
   Const sComputer = "."
   Dim sRet
   Dim sKeyPath
   Dim sValueName
   Dim sValue
   Dim objReg
   sRet = ""
 
   Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\default:StdRegProv")
   If (IsObject(objReg)) Then
      sKeyPath = "SOFTWARE\JavaSoft\Java Runtime Environment"
      sValueName = "CurrentVersion"
      If (RegValueExists(HKLM, sKeyPath, sValueName)) Then
         If (objReg.GetStringValue(HKLM, sKeyPath, sValueName, sValue) = 0) Then
            If (Len(sValue) > 0) Then
               sKeyPath = sKeyPath & "\" & sValue
               sValueName = "JavaHome"
 
               If (RegValueExists(HKLM, sKeyPath, sValueName)) Then
                  If (objReg.GetStringValue(HKLM, sKeyPath, sValueName, sValue) = 0) Then
                     If (Len(sValue) > 0) Then
                        sRet = sValue
                     End If
                  End If
               End If
            End If
         End If
      End If
   End If
 
   Set objReg = Nothing
   GetJavaPath = sRet
End Function

And there you have it! If no JRE is installed, the returned path will be a blank string. Otherwise, the path to the latest JRE will be returned (Example: C:\program files\Java\jre6). Please, note that the installation path is what is returned, not the full path to any executable.  Now with this logic, we can also do an existence check by simply checking the output of GetJavaPath() for a blank string, then check if non-empty string contains a valid path:

Function IsJavaInstalled()
   Dim objFSO
   Dim sPath
 
   sPath = GetJavaPath()
   If (Len(sPath) > 0) Then
      Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
      If (IsObject(objFSO)) Then
         If (objFSO.FolderExists(sPath)) Then
            IsJavaInstalled = True
         End If
      End If
      Set objFSO = Nothing
   Else
      IsJavaInstalled = False
   End If
End Function

Hope this helps someone out there! If anyone knows a better or more accurate/efficient way to do this, feel free to share!

Happy coding.