OS X Java version changing script

May 15th, 2006 by Hen

I’m sure it’s going to hit issues somewhere, and such probably already exist, but the following seemed worth sharing for switching Java version on a Mac.

function jvm() {
    if [ -d  /System/Library/Frameworks/JavaVM.framework/Versions/$1 ]
    then
        export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/$1/Home/
        PATH=$JAVA_HOME/bin:$PATH
    else
        echo "No such JVM: $1"
    fi
}

function jvms() {
    ls -1 /System/Library/Frameworks/JavaVM.framework/Versions/ | grep '^[0-9]'
}

Usage is:

cuckoo:hen$ jvms
1.3
1.3.1
1.4
1.4.2
1.5
1.5.0
cuckoo:hen$ jvm 1.5.0
cuckoo:hen$

2 Responses to “OS X Java version changing script”

  1. Ian Holsman Says:

    Thanks!
    I was looking for something like this so I could run opentaps on my laptop (which required 1.4.2) and solr (which needs 1.5)

  2. Jan Mat?rne Says:

    Maybe not as dynamic as yours (and for Windows) - but used the same technique for over a year and it works well. So I could compile Ant on a 1.2 or a 1.5 base (nice to see all the 1.5-deprecated stuff which was ‘new’ in 1.2).

    @echo off
    set JAVA_BASE=c:\jdk
    if “%1″==”help” goto help

    set JHOME=%JAVA_BASE%\%1
    if “%1″==”160″ set JHOME=%JAVA_BASE%\160_2004Nov
    if “%1″==”150″ set JHOME=%JAVA_BASE%\150
    ….
    if “%1″==”" set JHOME=C:\Programme\Java\j2sdk1.4.2_04
    goto end

    :help
    echo setjava - Set the Java-Version
    echo .
    echo 1.4.2_04
    echo 15 160 160_snap 1.6.0-Snapshot
    echo 15 150 150_b2 1.5.0

    goto end2

    :end
    if exist %JHOME%\bin\javac.exe goto setJavaHome
    echo %JHOME%\bin\javac.exe not found. No changes done.
    goto end2

    :setJavaHome
    set JAVA_HOME=%JHOME%
    call cleanPath.pl PATH _clean_.bat -add %JAVA_HOME%\bin -del jdk
    call _clean_.bat
    del _clean_.bat
    echo Java-Home: %JAVA_HOME%
    call java -version
    goto end2

    :end2
    set JAVA_BASE=
    set JHOME=

    Oh the ‘cleanPath.pl’ is a small perl skript which can add and remove entries (regexp based) from the PATH variable. Nice to clean that stuff …