0
votes

I'm trying to determine where is located Java provided by Oracle on machine. I have several location where java is located but my script should be able to find appropriate (Oracle Java) version using simple script:

{$java_location}/bin/java.exe -XshowSettings:properties -version | findstr java.vendor | findstr oracle

This should return empty output when not Oracle Java and some text when it's the right one. Unfortunately VBScript hangs up when calling this commands.

Set oExec = WSHShell.Exec({$java_location}/bin/java.exe -XshowSettings:properties -version | findstr java.vendor | findstr oracle > " & "c:\Users\J33nn\testscript")
Do While oExec.Status = 0
  WScript.Sleep 100
Loop

Is there any way to run this and prevent script from hanging? Or any other way to run this command from VBS script?

3

3 Answers

0
votes

I've found a workaround. Since I can't use Exec to read output directly I just pass output from command to temporary file and read it after command execution.

WSHShell.Run "%comspec% /c ""{$java_location}/bin/java.exe"" -XshowSettings:properties -version | findstr java.vendor | findstr oracle > " & "c:\Users\J33nn\testscript", 0, TRUE
0
votes

And this lead me to perfect solution :)

WSHShell.Exec("%comspec% /c ""{$java_location}/bin/java.exe"" -XshowSettings:properties -version | findstr java.vendor | findstr oracle")

It seems that running with %comspec% /c does the magic in this case.

0
votes

You should be able to read the location of the Java installation from the registry like this:

Const HKLM = &h80000002

vendor  = "Oracle"
basekey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

Set reg = GetObject("winmgmts://./root/default:StdRegProv")

rc = reg.EnumKey(HKLM, basekey, subkeys)
If rc = 0 Then
  For Each key In subkeys
    subkey = baskey & "\" & key
    rc = reg.GetStringValue(HKLM, subkey, "Publisher", publisher)
    If rc = 0 Then
      If publisher = vendor Then
        rc = reg.GetStringValue(HKLM, subkey, "InstallLocation", location)
        If rc = 0 Then javaDir = location
      End If
    End If
  Next
End If

WScript.Echo javaDir