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?

user938644
  • 13
  • 1
  • 1
  • 4
  • 1
    `if "%JAVA_HOME%"=="" (` ? – Akina Aug 30 '18 at 09:48
  • @Akina same issue – user938644 Aug 30 '18 at 10:50
  • 1
    Possible duplicate of [finding location java\_home in Windows 10](https://superuser.com/questions/1148677/finding-location-java-home-in-windows-10) – LotPings Aug 30 '18 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 Aug 30 '18 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 Aug 30 '18 at 12:52
  • @Akina Where is the file located? what is the path return? it needs place it in C:\Program Files\Common Files\Services for example and test ( path needs to have at least 2 spaces in it) – user938644 Aug 30 '18 at 13:02
  • Which path do you mean? JAVA.EXE? it is `C:\Program Files (x86)\Common Files\Oracle\Java\javapath`. – Akina Aug 30 '18 at 13:07
  • @Akina sorry but i tried your suggestion and it displays \Common was not expected – user938644 Aug 30 '18 at 13:20
  • Because of long text the comment is posted as an answer. – Akina Aug 30 '18 at 13:35

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,195
  • 1
  • 8
  • 9
  • copied your comparison statement as is --> result : https://pasteboard.co/HBzEzS7.png – user938644 Aug 30 '18 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 Aug 30 '18 at 13:54
  • Sorry for the late reply, please check the link https://pasteboard.co/HBYEBsa.png – user938644 Sep 02 '18 at 05: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"
  • The patch is fetched dynamically, set JAVA_HOME=%%~dp$PATH:j , how do you add "" to it – user938644 Sep 03 '18 at 06:10
  • How was the path set to begin with? That's where you would apply the double quotes. – John Czukkermann Sep 03 '18 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 Sep 06 '18 at 05:14