0

I'm trying to fetch my java jdk path using a batch script. Here is what i have so far:

@echo off
for /f %%j in ("java.exe") do (
    set JAVA_HOME=%%~dp$PATH:j
)

if "%JAVA_HOME%".==. (
    @echo Java.exe not found
    @echo Please make sure that java JDK 1.7 or 1.8 is installed
)

In the if statement, since the path is in C:\Program Files\Common Files\etc.., i receive the error Common was not expected at this time. However, the IF condition does not work properly if Java is not found.

If i remove the "" surrounding JAVA_HOME, i get an error that Files was not expected at this time. In this case the IF condition works properly if JAVA.exe is found.

Why is it allowing the space or the '\' in \Program Files and stopping afterwards?

2 Answers2

0

i tried your suggestion and it displays \Common was not expected

Copy from console:

C:\tmp>del test.bat

C:\tmp>copy con test.bat
@echo off
for /f %%j in ("java.exe") do (
    set JAVA_HOME=%%~dp$PATH:j
)

if "%JAVA_HOME%"=="" (
    @echo Java.exe not found
    @echo Please make sure that java JDK 1.7 or 1.8 is installed
) else (
    @echo Java.exe exists in "%JAVA_HOME%"
)
^Z
Files copied:         1.

C:\tmp>test
Java.exe exists in "C:\Program Files (x86)\Common Files\Oracle\Java\javapath\"

Emulate the absence - JAVA.EXE replaced with ZZZ.EXE:

C:\tmp>del test.bat

C:\tmp>copy con test.bat
@echo off
for /f %%j in ("ZZZ.exe") do (
    set JAVA_HOME=%%~dp$PATH:j
)

if "%JAVA_HOME%"=="" (
    @echo Java.exe not found
    @echo Please make sure that java JDK 1.7 or 1.8 is installed
) else (
    @echo Java.exe exists in "%JAVA_HOME%"
)
^Z
Files copied:         1.

C:\tmp>test
Java.exe not found
Please make sure that java JDK 1.7 or 1.8 is installed

C:\tmp>
Akina
  • 3,295
0

When you construct the path, put double quotes " around each individual entry in the path.

For example:

export XPATH="c:\Program Files\foo":"C:\Program Files\bar"