1

The third FOR statement in the code below is supposed to return '18066' from the command output from 'java full version "1.8.0_66-b18" but it is returning blank, which causes the comparison that follows to fail. Can anyone offer some help?

@echo off

set MSIFileName=jre1.8.0_66.msi
set KeyName="HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\javaws.exe"

for /f tokens^=2-5^ delims^=e._m^" %%a in ("%MSIFilename%") do set VersionToInstall=%%a%%b%%c%%d

for /f "tokens=2*" %%h in ('reg query %KeyName% /v Path') do set JavaHome=%%i
PATH "%PATH%;%JavaHome%"

for /f tokens^=2-5^ delims^=.-_^" %%j in ('java -fullversion 2^>^&1') do set InstalledVersion=%%j%%k%%l%%m
if "%InstalledVersion%."=="." goto INSTALL
if "%InstalledVersion%" GEQ "%VersionToInstall%" goto END

TIA for the replies.

Note: I borrowed some code from user npocmaka found here.

-EDIT-

I still don't have an answer for why the original code didn't work but I found a workaround that has my script working. Rather than adding JavaHome to system PATH so the script could find java.exe I used 'cd' instead. In hope that this helps someone else here's the updated code...

@echo off

set MSIFileName=jre1.8.0_66.msi
set KeyName="HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\javaws.exe"
set JavaHome=0
set IsNumber=0

for /f tokens^=2-5^ delims^=e._^" %%a in ("%MSIFilename%") do set VersionToInstall=%%a%%b%%c%%d

for /f "tokens=2*" %%e in ('reg query %KeyName% /v Path') do set JavaHome="%%f"

if %JavaHome% EQU 0 goto INSTALL

cd %JavaHome%

for /f tokens^=2-5^ delims^=._-^" %%g in ('java -fullversion 2^>^&1') do set InstalledVersion=%%g%%h%%i%%j

for /f "tokens=* delims=0123456789" %%k in ("%InstalledVersion%") do (
    if "[%%k]" EQU "[]" set IsNumber=1
)

if %IsNumber% EQU 0 goto INSTALL

if "%InstalledVersion%" GEQ "%VersionToInstall%" goto END
Community
  • 1
  • 1
JasonBA
  • 21
  • 2
  • 1
    Have you cut-and-pasted the code, or did you retype it? Batch is sensitive to spaces sometimes, for instance in a `set` statement on **both** sides of the `=`. What does the `java` query return to the console when run independently? The active codeline `for /f ... ^^j... ` worked happily for me, setting `installedversion` appropriately. – Magoo Feb 11 '16 at 08:27
  • also, for loop parameters are case sensitive, its safer to use and start at `%%G` instead of `%%g`, in fact you using `%%a` may be a likely error, try a different character. – Bloodied Feb 11 '16 at 15:43
  • I'll try a different character but that's news to me that uppercase characters are preferred. It's the third for loop that's giving me fits, the one using %%j%%k%%l%%m. – JasonBA Feb 12 '16 at 04:16
  • Magoo, I'll also try retyping it. I can't recall whether I pasted it but the third for loop is a slight variation on the first and the first works without issue. It's odd that it's working for you. – JasonBA Feb 12 '16 at 04:20
  • If `InstalledVersion` is empty, it means than java is not in the `PATH`. It means that the `JavaHome` does not contain the `java.exe`. I would use another registry path instead. The children of `HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit` or `HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime` Environment are installed JDKs and JREs. – Amnon Feb 15 '16 at 07:27
  • Thanks for the reply. Not sure if I'm special but neither of those keys exist in my registry. "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\javaws.exe" is the only key I could find without a version number in it that points to 'C:\Program Files (x86)\Java\jre\bin'. I'd rather not use a key with a CLSID in it either. You could be right about JavaHome being the culprit but now that I abandoned adding JavaHome to %PATH% in favor of good ol' fashoined cd my script is working. – JasonBA Feb 17 '16 at 23:05

1 Answers1

0
for /f "tokens=4 delims= " %%j in ('java -fullversion 2^>^&1') do set InstalledVersion=%%~j
echo %InstalledVersion%

works happily for me.

1.8.0_73-b02

from my version

java full version "1.8.0_73-b02"

Magoo
  • 77,302
  • 8
  • 62
  • 84