burnersk
burnersk

Reputation: 3480

How to call a program with parameters from batch script?

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.

Upvotes: 2

Views: 487

Answers (1)

halex
halex

Reputation: 16403

Changing the line

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

to

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

it worked for me.

Upvotes: 6

Related Questions