5
votes

I need to switch the Java version on my Mac OS X 10.8.4 but am not sure how, the version I have right now is 1.6.0_51 but i want to switch to 1.6.0_45:

$java -version
java version "1.6.0_51"
Java(TM) SE Runtime Environment (build 1.6.0_51-b11-457-11M4509)
Java HotSpot(TM) 64-Bit Server VM (build 20.51-b01-457, mixed mode)

/usr/libexec/java_home -v 1.6.0_43
Unable to find any JVMs matching version "1.6.0_43".
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

I assume there is no such a version as 1.6.0_43 on my mac? how can install it and then switch to it? thanks!

4
A (later) 1.6.0 Java version should be compatible in every way with an older version from the 1.6.0 series, shouldn't it? What are you trying to accomplish?Danilo Piazzalunga
You said you want to switch to 1.6.0_45 but you search for 1.6.0_43... which one is it?2rs2ts
There is a command line option that allows you to set the correct JVM (-target<release> and source<release>)aaronman
i need to run an application that runs well with java 1.6.0_43 but not with 1.6.0_51. 43 or 45 both works, it's a typo.user468587

4 Answers

11
votes

To see which versions you have installed use:

/usr/libexec/java_home -V

For example shows this for me:

15:04 $ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    10.0.1, x86_64: "Java SE 10.0.1"    /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
    1.7.0_80, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home

Then simply in your ~/.bashrc or ~/.bash_profile:

$ export JAVA_HOME=$(/usr/libexec/java_home -v 1.7.0_80)

As for getting version 1.6 (or nearly any prev version) I think that as of this date (Nov 2018) you will be out of luck. I can get to https://jdk.java.net/8/ but any earlier version is a 404 (11 is the latest). I'm sure you could search a bit more though as I understand it, once they (Oracle) have discontinued a version, they no longer maintain it since it could be a security risk.

Once you have DL a Mac .dmg, I'm sure it installs in the correct location and will be visible with java_home.

There is information at https://stackoverflow.com/a/47699905/1019307 that discusses using homebrew to install Java and it mentions Java6 (the post is dated Dec 2017).

4
votes

As the description says on this website: "...it’s really a pain to switch between this Java version by shell..." so this person made a tool to do the switching. Since I don't know your reasoning (if you really have to do it via the command line) it may not be overly useful but a quick google search revealed this downloadable tool from their website.

1
votes
  1. Install an other Java version, that you need.

  2. I had to switch between 1.6_XX and 1.7_XX so wrote a litte script for that. It does its job pretty good. You have to replace some stuff but maybe it helps you:

    #!/bin/bash
    #v1.0.3
    #by Sapphire
    
    # /!\ Configuration /!\
    # vim ~/.bash_profile :
    # alias changej="sudo ~/Documents/changeJavaVers.sh"
    # sudo visudo -f /etc/sudoers :
    # #Custom privilege
    # user ALL=NOPASSWD: /Users/username/Documents/changeJavaVers.sh
    # change java dir's at (1) and (2)
    
    echo -e "\n***********************************************************"
    echo "*                EASY JAVA VERSION CHANGER                *"
    echo "***********************************************************"
    
    echo "Enter java version (6, 7, ..) or 'set' for settings"
    echo -n "Version: "
    read vers
    if [ "$vers" == "set" ]
    then
        echo "settings not implemented yet"
        #TODO open settings
        #set java home path, add java versions
        #current home: echo $JAVA_HOME
    elif (($vers == 6))
    then
        #(1)
        ln -fhsv /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
        export JAVA_HOME=`/usr/libexec/java_home -v 1.6+ -d32`
        echo -e "\n-----------------------------------------------------------"
        echo "                       JAVA VERSION"
        echo "-----------------------------------------------------------"
        java -version
        echo -e "\n-----------------------------------------------------------"
        echo "                        JAVA HOME"
        echo "-----------------------------------------------------------"
        echo $JAVA_HOME
        echo "***********************************************************"
    elif (($vers == 7))
    then
        #(2)
        ln -fhsv /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
        export JAVA_HOME=`/usr/libexec/java_home`
        echo -e "\n-----------------------------------------------------------"
        echo "                       JAVA VERSION"
        echo "-----------------------------------------------------------"
        java -version
        echo -e "\n-----------------------------------------------------------"
        echo "                        JAVA HOME"
        echo "-----------------------------------------------------------"
        echo $JAVA_HOME
        echo -e "\n***********************************************************"
    
    else
        echo -e "\ninvalid input - available java versions: 6, 7"
    fi
    

EDIT Got a newer version here. Works for me on OSX 10.9.3

#!/bin/bash
#v1.1.0
#last change: 12 June 2014
#by Sapphire

# /!\ Configuration /!\
# vim ~/.bash_profile :
# alias changej="sudo ~/Documents/changeJavaVers.sh"
# sudo visudo -f /etc/sudoers :
# #Custom privilege
# user ALL=NOPASSWD: /Users/username/Documents/changeJavaVers.sh
# change java directories (1), (2)

echo -e "\n***********************************************************"
echo "*                EASY JAVA VERSION CHANGER          v1.1.0 *"
echo "***********************************************************"

echo "Enter java version (6, 721, 745, 760) or 'set' for settings"
echo -n "Version: "
read vers
if [ "$vers" == "set" ]
then
    echo "settings not implemented yet"
    #TODO open settings
    #set java home path, add java versions
    #current home: echo $JAVA_HOME
elif (($vers == 6))
then
    #(1)
    ln -fhsv /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
elif (($vers == 721))
then
    #(1)
    ln -fhsv /Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
elif (($vers == 745))
then
    #(1)
    ln -fhsv /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
elif (($vers == 760))
then
    #(1)
    ln -fhsv /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/ /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
else
    echo -e "\ninvalid input - available java versions: (6, 721, 745, 760)"
    exit
fi
#(2)
export JAVA_HOME='/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home'
echo -e "\n-----------------------------------------------------------"
echo "                       JAVA VERSION"
echo "-----------------------------------------------------------"
java -version
echo -e "\n-----------------------------------------------------------"
echo "                        JAVA HOME"
echo "-----------------------------------------------------------"
echo $JAVA_HOME
echo -e "\n***********************************************************"
exit
0
votes

There is no need to download a whole new version. What you really want is this:

-target version
Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), and 1.7 (also 7).
The default for -target depends on the value of -source:

If -source is not specified, the value of -target is 1.7
If -source is 1.2, the value of -target is 1.4
If -source is 1.3, the value of -target is 1.4
If -source is 1.5, the value of -target is 1.7
If -source is 1.6, the value of -target is 1.7
For all other values of -source, the value of -target is the value of -source.  

from this source. -target and -source allow to control the target JVM and source code level. As the source says the value of target will default to the value you give source but you can control both if you want.

If this option can't provide a specific enough version for you, though I can't imagine why you would need such an exact version, then you'll just have to bite the bullet and download the right version yourself