Tuesday, January 03, 2012

Batch script to change Java Home between different JDKs

I recently happened to be working on multiple releases with some of them requiring the code to be compiled with JDK 6 whereas the more recent releases requiring code to compile with JDK 7. So i wrote up the following batch script to make the job of switching between the 2 JDKs easier. Before i run my ant build script i run the below script to set to ‘7’ for JDK 7 or ‘6’ for JDK 6.

@ECHO OFF
echo %1%
 
if "%1"== "" ( 
    echo "Syntax: javahome 7 or 6" 
    Goto :end
) 
if "%1" == "7" (
    echo "Setting JDK 7"
    
    set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_01
    GOTO :printver
) 
 
if "%1" == "6" (
    echo "Setting JDK 6"
    
    set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_23
    GOTO :printver
)
 
:printver
echo %JAVA_HOME%
 
set NEW_PATH=%PATH%
FOR /F "delims=;" %%P IN ("%PATH%") DO (
    @ECHO path = %%~P
 
)
 
 
set PATH=%JAVA_HOME%\bin;%NEW_PATH%
echo %PATH%
 
java -version
:end

No comments: