thotheolh
thotheolh

Reputation: 7440

Batch file to execute a java application

I have a java application I would like my Windows batch file to execute. May I know why the following batch file codes do not work and how I can correct them ? The script should check for 32bit Java first before proceeding to check for 64 bit Java.

I would also like my batch file to handle Java 6 and above versions and inclusive of JRE or JDK environments. How would I modify my batch file to handle them.

Batch Script:

@ECHO OFF 
IF EXIST "C:\Program Files (x86)\Java" (
    start C:\Program Files (x86)\Java\jre7\bin\java -jar %~dp0\JavaShop.jar
) ELSE (
    IF EXIST "C:\Program Files\Java" C:\Program Files\Java\jre6\bin\java -jar %~dp0\JavaShop.jar
    ELSE ECHO Java software not found on your system. Please go to http://java.com/en/ to download a copy of Java.
    PAUSE
)

Upvotes: 0

Views: 5695

Answers (2)

Efthymis
Efthymis

Reputation: 1326

You have space characters in your execution path. Try this

@ECHO OFF 
IF EXIST "C:\Program Files (x86)\Java\jre7" (
    start "C:\Program Files (x86)\Java\jre7\bin\java.exe" -jar %~dp0\JavaShop.jar
) ELSE (
    IF EXIST "C:\Program Files\Java\jre6" 
    start "C:\Program Files\Java\jre6\bin\java.exe" -jar %~dp0\JavaShop.jar
    ELSE ECHO Java software not found on your system. Please go to http://java.com/en/ to download a copy of Java.
    PAUSE
)

The best thing to do though is to check if the environmental variable JAVA_HOME is set. If it is set, then java is installed in the system.

@ECHO OFF 
IF EXIST %JAVA_HOME% (
    start %JAVA_HOME%\bin\java.exe -jar %~dp0\JavaShop.jar
) ELSE (
    ECHO Java software not found on your system. Please go to http://java.com/en/ to download a copy of Java.
    PAUSE
)

If you don't have a JAVA_HOME set you could just try the java command itself.

@ECHO OFF 
IF EXIST java (
    start java -jar %~dp0\JavaShop.jar
) ELSE (
    ECHO Java software not found on your system. Please go to http://java.com/en/ to download a copy of Java.
    PAUSE
)

Upvotes: 1

Brian
Brian

Reputation: 6450

I think you're onto a loser if you try to anticipate all likely install paths. Surely if Java's available on the machine, it's already on its path, i.e. available via just:

java

Also in your "start" line, and assuming a hardcoded path was good enough, you would need " chars around the path, due to the space character in it.

Upvotes: 6

Related Questions