2

I like to determine which Java runtime environments are available and check their versions. My code will output the correct jre paths but is unable to execute the java.exe.

What is the correct way to call a program with parameters from a batch script while the program path is a variable?

My script

@echo off


:findjres
for /d %%i in ("C:\Program Files\Java\jre*") do (
    set /a cpath+=1
    call :pushpath "%%i"
)
for /d %%i in ("C:\Program Files (x86)\Java\jre*") do (
    set /a cpath+=1
    call :pushpath "%%i"
)
goto :versjres


:pushpath
set tpath=%1
set xpath!%cpath%=%tpath:~1,-1%
goto :end

:versjres
for /f "usebackq delims==! tokens=1-3" %%i IN (`set xpath`) do (
    echo Array field number %%j have value %%k
    %%k\bin\java.exe --version
)

pause


:end

The error

Exception in thread "main" java.lang.NoClassDefFoundError: Files
Caused by: java.lang.ClassNotFoundException: Files
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Files.  Program will exit.
0

1 Answer 1

6

Changing the line

%%k\bin\java.exe --version

to

"%%k\bin\java.exe" -version

it worked for me.

Sign up to request clarification or add additional context in comments.

2 Comments

Awww, I am stupid - you are right. This happens if you work to long. Thank you, @halex!
Given the error message above: "Could not find main class: Files.", I presume that java is stored in one of C:\Program Files or `C:\Program Files (x86)', the spaces are deliminating directory levels. Hence why the quotes works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.