1.

Solve : Testing for JRE?

Answer»

Hello all

I need to test if the Java Runtime Environment is installed on the local computer before running a program. If it isn't I default to another version of the program.

Any ideas on how I would do that?
Surprisingly you can use Java to determine if the JRE is installed.

Code: [Select]@echo off
for /f "tokens=* delims=" %%v in ('java -version 2^>^&1 ^| find /i "runtime"') do (
if errorlevel 1 echo %%v && GOTO :eof
)
echo Java NOT installed

A little misdirection redirection, some SMOKE and mirrors and you've got an instant cup of coffee.

Ok I tried to modify this to get myself a cup of cofee and am having a problem.

Here is my script:

REM Test to see if the Java Runtime is installed on the local computer
if {%MYQKC%}=={qkj} (
for /f "tokens=* delims=" %%v in ('java -version 2^>^&1 ^| find /i "runtime"') do (
if errorlevel 1 set MYQKC=qkc
)
)

Here is the executed command:

C:\>REM Test to see if the Java Runtime is installed on the local computer
C:\>if {qkj} == {qkj} (for /F "tokens=* delims=" %v in ('java -version 2>&1 | fi
nd /i "runtime"') do (if errorlevel 1 set MYQKC=qkv ) )

C:\>pause

Shouldn't the errorlevel check be a variable?
As in:

if {%MYQKC%}=={qkj} (
for /f "tokens=* delims=" %%v in ('java -version 2^>^&1 ^| find /i "runtime"') do (
if %ERRORLEVEL% NEQ 0 set MYQKC=qkc
)
)

Either way it is not setting the variable correctly.
HELP....Was there something wrong with the POSTED code. I thought it was rather clever! If I read your modifications correctly, this may help you out:

Code: [Select]@echo off
for /f "tokens=* delims=" %%v in ('java -version 2^>^&1 ^| find /i "runtime"') do (
if errorlevel 1 set myqkc=qkc && goto :next
)
set myqkc=qkv

:next
.
. your code goes here
.

By replacing your code goes here with %myqkc%, the appropriate program will run.

Good luck. I copied your code into my script and nothing happens. This computer has Java installed so I expected the variable to change to qkc:

SCRIPT:
@echo ON
echo %MYQKC%

REM Test to see if the Java Runtime is installed on the local computer
for /f "tokens=* delims=" %%v in ('java -version 2^>^&1 ^| find /i "runtime"') do (
if errorlevel 1 set MYQKC=qkc && goto :next
)
set myqkc=qkv

:next

echo %MYQKC%
pause


RESULTS:

C:\>echo qkj
qkj

C:\>REM Test to see if the Java Runtime is installed on the local computer

C:\>for /F "tokens=* delims=" %v in ('java -version 2>&1 | find /i "runtime"') d
o (if errorlevel 1 set MYQKC=qkc && goto :next )

C:\>(if errorlevel 1 set MYQKC=qkc && goto :next )

C:\>set myqkc=qkv

C:\>echo qkv
qkv

C:\>pause
Press any key to continue . . .Ok nevermind I got it. I changed the script to store the contents of the errorlevel and tested for that:

REM Test to see if the Java Runtime is installed on the local computer
if {%MYQKC%} NEQ {qkj} goto NOTJAVA
for /f "tokens=* delims=" %%v in ('java -version 2^>^&1 ^| find /i "runtime"') do (
set JRETEST=%ERRORLEVEL%
)
if {%JRETEST%} NEQ {0} set MYQKC=qkv
:NOTJAVA



Discussion

No Comment Found