May 16, 2009

Set JAVA_HOME in a Windows Command Script

Apache Ant is generally the preferred tool for building Java applications, and most IDEs support building with Ant right from the IDE. However, when you need to build your Java application outside of the IDE, it can be handy to have a script kick off Ant.

On Microsoft Windows the script needs to know the location of the JDK and Ant, which generally entails:
  • Relying on environment variables to be set correctly or
  • Hardcoding the path locations

Both approaches are brittle and likely to result in maintenance and support headaches down the road. Additionally, hardcoding the paths assumes all the developers are running the same versions of the JDK and Ant. These limitations are insignificant for a centrally managed commercial software organization, but they can be problematic for a distributed open source project.

DOS For Command
Fortunately, it's easy to write a Command Script (.cmd) batch file using the DOS for command that automatically determines the path to the most current versions of the JDK and Ant. To test out the for command, try the following at the Windows Command Prompt.

Command Prompt Demonstration:
C:\>for /d %i in ("\Program Files\Java\jdk*") do set JAVA_HOME=%i
C:\>set JAVA_HOME

You should see a result similar to:
       JAVA_HOME=\Program Files\Java\jdk1.6.0_13

Command Script Solution
To make it easy to launch Ant, put the following build.cmd file into the same folder as your build.xml Ant configuration file.

The build.cmd File:
@echo off
::::::::::::::::::::::::::::::::::::
:: build.cmd (v1.0) ::
:: Microsoft Windows Build Script ::
::::::::::::::::::::::::::::::::::::

:: Set JAVA_HOME
for /d %%i in ("\Program Files\Java\jdk*") do set JAVA_HOME=%%i

:: set ANT_HOME
for /d %%i in ("\Apps\Ant\apache-ant*") do set ANT_HOME=%%i

:: Display Variables and Launch Ant
set JAVA_HOME
set ANT_HOME
call %ANT_HOME%\bin\ant build
pause

View/Download: build.cmd

Now to build your project, double-click the build.cmd file.

While the script does not hardcode the JDK and Ant versions, it does require that the JDK is installed into the default location and that Ant in installed (copied) into the \Apps\Ant folder.

Alternative Approach
If you prefer to be more robust in your detection of the JDK home, you can use the DOS reg query command to read the Windows Registry information to locate the JDK. In the build.cmd file, replace the line that sets JAVA_HOME with the lines below.

Use Windows Registry to Set JAVA_HOME:
:: Set JAVA_HOME
set KeyName=HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit
set Cmd=reg query "%KeyName%" /s
for /f "tokens=2*" %%i in ('%Cmd% ^| find "JavaHome"') do set JAVA_HOME=%%j

View/Download: set-java-home.cmd

The output of the reg query is piped through find to single out the desired key value. The third token (represented by %%j) on the line is the path.

Simplify
By having build.cmd auto-detect the JDK and Ant, you can simplify the steps for new developers looking to contribute to your open source project.

Mac OS X Shell Script
To kick off Ant on a Mac, you can use this shell script:
#!/bin/sh
#############################
## Mac OS X Build Script ##
#############################

cd `dirname $0`
ant build

View/Download: build.sh.command

This script is simple because Apple bundles the JDK and Ant with Mac OS X.


0 comments: