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?

9
  • 1
    if "%JAVA_HOME%"=="" ( ?
    – Akina
    Commented Aug 30, 2018 at 9:48
  • @Akina same issue
    – user938644
    Commented Aug 30, 2018 at 10:50
  • 1
    Possible duplicate of finding location java_home in Windows 10
    – LotPings
    Commented Aug 30, 2018 at 11:46
  • My question is not related to that @LotPings , i have a problem displaying a path not getting the java_home path (which is already done)
    – user938644
    Commented Aug 30, 2018 at 12:44
  • same issue Your code with my addition was tested on Win7x86, Win7x64, Win8.1x64, Win10x64. It works correctly on all versions (to emulate the absence of executable the name was changed from JAVA.EXE to some ZZZ.EXE).
    – Akina
    Commented Aug 30, 2018 at 12:52

2 Answers 2

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>
3
  • copied your comparison statement as is --> result : pasteboard.co/HBzEzS7.png
    – user938644
    Commented Aug 30, 2018 at 13:50
  • @user938644 Please add some echo as a first line in else secion, before FOR /F ... line for to make sure the problem message is produced by compare operator.
    – Akina
    Commented Aug 30, 2018 at 13:54
  • Sorry for the late reply, please check the link pasteboard.co/HBYEBsa.png
    – user938644
    Commented Sep 2, 2018 at 5:27
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"
3
  • The patch is fetched dynamically, set JAVA_HOME=%%~dp$PATH:j , how do you add "" to it
    – user938644
    Commented Sep 3, 2018 at 6:10
  • How was the path set to begin with? That's where you would apply the double quotes. Commented Sep 3, 2018 at 12:00
  • This is where the variable is set, how do you add double quotes to it set JAVA_HOME=%%~dp$PATH:j --- if you add before the % it will take the whole thing as a string instead of fetching the path
    – user938644
    Commented Sep 6, 2018 at 5:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .