0

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
)
10
  • 1
    Out of curiosity, why 32bit first? 32bit is basically dead Commented Dec 22, 2011 at 13:06
  • didnt understand your question properly, do u want to execute a batch file from your java program? or You are trying to run your JavaShop.jar from your batch file? Commented Dec 22, 2011 at 13:06
  • 2
    What's the error here? Maybe names with spaces are a problem. Try to "double quote" them. Commented Dec 22, 2011 at 13:09
  • Please tell us what does not work and how you know it does not work. Commented Dec 22, 2011 at 13:10
  • when you execute this script what happens? does "C:\Program Files (x86)\Java\jre7\bin\java -jar %~dp0\JavaShop.jar" work from the prompt (outside any script)? Commented Dec 22, 2011 at 13:10

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1; e.g. I do never install my JDKs, JREs in default location.
I installed Java JDK as 64 bit and when I go to my command line and type java, it tells me it is not recognized. I need to specifically go to the 64 bit Java to execute it otherwise it wouldn't be recognized.
if you can't go to C:\ and run "java", then it's not on your machine's path. For more info here, read: java.com/en/download/help/path.xml
1

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
)

3 Comments

I ran it and it says the syntax is incorrect without specifying.
@thotheolh which version? the JAVA_HOME one?
I noticed that my JAVA_HOME is not set despite Java 7 already installed in the system. Both versions had syntax incorrect errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.