Explain Codes LogoExplain Codes Logo

Installed Java 7 on Mac OS X but Terminal is still using version 6

java
java-installation
environment-variables
java-home
Alex KataevbyAlex Kataev·Aug 25, 2024
TLDR

Activate Java 7 in Terminal in a flash:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.7) export PATH=$JAVA_HOME/bin:$PATH

Use these commands directly or append them to ~/.bash_profile (for Bash) or ~/.zshrc (for Zsh). This ensures Java 7 is the default in every Terminal session.

Grasping the issue

You've installed Java 7 but the Terminal sticks to the ancient version 6. This occurs due to a mismatch between the system's PATH configuration or a misdirected JAVA_HOME environment variable.

Verify your Java version

Check that the installation of Java 7 was successful:

/usr/libexec/java_home -V

This lists all JDKs on your system - verify Java 7 is included.

Weed out the old JAVA_HOME

Scan your .bash_profile or .bashrc for lines that set the JAVA_HOME variable to the old version:

grep "JAVA_HOME" ~/.bash_profile

Remove or update these lines to reflect the correct Java 7 path.

Fixing the environment settings

Now that we've cleaned up, it's time to set the new environment settings.

Cleaning up the .bash_profile

Open ~/.bash_profile in a text editor and replace any outdated Java paths:

# Wipeout the old path unset JAVA_HOME # Setup the super path to Java 7 export JAVA_HOME=$(/usr/libexec/java_home -v 1.7) export PATH=$JAVA_HOME/bin:$PATH

Our system still thinks /usr/bin/java is version 6. Let's bring it up to speed:

# Mr. Old Link, your time has come sudo rm /usr/bin/java # Sagacious New Link, take your place! sudo ln -s $(/usr/libexec/java_home -v 1.7)/bin/java /usr/bin/java

After, rerun java -version to confirm Java 7 is now the go-to version.

Covering all bases

Let's ensure Java 7 is properly sown system-wide.

Favor JDK, not JRE

For all the development tools, get the full JDK if you haven't already:

# Yes, it's the Oracle's official website open https://www.oracle.com/java/technologies/downloads/

Juggling multiple Java versions

Alternating between Java versions frequently? Here's a swindler's guide to it: use jEnv:

# Coffee brews, so does code! brew install jenv # Add another feather to jEnv's cap. This time, it's Java 7 jenv add $(/usr/libexec/java_home -v 1.7)

Setting system preferences

You may find the System Preferences still favoring Java 6. To pick the fresher version, navigate to the Java control panel:

# Apple's secret access to the Java Control Panel! open -a "Java Preferences"

Uncheck the boring old Java 6 and check the shiny new Java 7.