Link Search Menu Expand Document

Making it so that you can dynamically load different versions of java on Mac

So you want to have multiple version of Java on your computer!

Fancy gif

Determine the current version of Java that you want to be able to access after you install the newer version

In terminal, type the following command:

java --version

Now you can go install the newer version of Java. It shouldn’t remove the current version, just overwrite the environmental variables so that the newer version of java is what will be called when you use java commands in the terminal.

Determine which shell prgram you are running.

What type of shell program are you running? You’ll need to determine this so we know what file to create/edit.

Do you have ZSH?

If you have ZSH, you’re probably on a newer Mac and/or have followed their instructions to change your shell program. The easiest way to tell that you likely are using zsh is to see each line beginning with %:

Do you have bash?

If you don’t have zsh you probably are running a version of bash. With bash, each line would likely begin with $:

Check your versions

You need to ensure you have java 8 JDK installed (you might have JRE!) /cmpsc497-fa23/assets/help/ Run the following command to check and make sure that you have a JDK version of java8 (note that we’re using uppercase v below)

export JAVA_HOME=`/usr/libexec/java_home -V

Ah! I can’t tell, it doesn’t seem to work or I installed Java in a non-typical way

You can try this other command I found in looking for alternatives

mdfind -name 'java' | grep '/bin/java$' 

With this command, you might see JRE directories with a bin, but this is only problematic if there aren’t alternative directories without a JRE as the parent directory to a listed bin folder. See below for mine as an example. (Notice for some java installs, we have a Home/bin and a Home/JRE/bin)

Create/edit the profile file

Got ZSH?

Navigate to your home directory:

cd ~

Run the following command in your terminal/bash:

nano .zshenv

Now that you have your bash profile opened up, you either are creating a new one, or there is already information in there. Either way, you just have to add a new line to it.

In the example below, I’m making it possible to dynamically load java 8 & java 11 (note that we’re using lowercase v below)

alias java8='export JAVA_HOME=`/usr/libexec/java_home -v 1.8`'
alias java11='export JAVA_HOME=`/usr/libexec/java_home -v 11`'

Got Bash?

Navigate to your home directory:

cd ~

Run the following command in your terminal/bash:

nano .bash_profile

Now that you have your bash profile opened up, you either are creating a new one, or there is already information in there. Either way, you just have to add a new line to it.

In the example below, I’m making it possible to dynamically load java 8 & java 11

alias java8='export JAVA_HOME=`/usr/libexec/java_home -v 1.8`'
alias java11='export JAVA_HOME=`/usr/libexec/java_home -v 11`'

Verify it works

Now, let’s verify it’s working

1: Open a new terminal window/tab

Open your terminal

2: Test current Java

Test your Java model

3: Try new java alias (the example turns on Java 8)

Turn your Java on

4: Test new Java out

Is it fine? Let's find out

And that’s it!

We done