Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Automatically Set JAVA_HOME in a .bat File

807588May 14 2009 — edited May 21 2009
Is there a way to automatically determine the current location of the JDK so that it can be used to set the JAVA_HOME environment variable?

Our project uses a very simple .BAT file to launch Ant on Micorosft Windows platforms.

Build.bat:
@echo off
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_07
set ANT_HOME=C:\Apps\Ant\apache-ant-1.7.1
call %ANT_HOME%\bin\ant build
pause
It works. However, the paths are hardcoded and the .bat file breaks every time a new verion of Java is released. I want to be able to set JAVA_HOME automatically.

If I type "*java -version*" at the command (DOS) prompt, I get information about the current instlation of Java. Presumably, that is using "*C:\WINDOWS\system32\java.exe*" and it knows the current version of Java. There has to be a way to do something similar to the Unix/Linux "*which java*" command.

Note that if Java is not yet installed on Windows, you get this:
C:\Documents and Settings\Dem>java -version
'java' is not recognized as an internal or external command,
operable program or batch file.
After installing the JDK, you get this:
C:\Documents and Settings\Dem>java -version
java version "1.6.0_13"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode, sharing)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 18 2009
Added on May 14 2009
17 comments
25,532 views