Because I move from project to project, I have to change JDKs. Some times I have to work on two projects at the same time. Or I am moving between POC and client work. What I need is a version manager. Every major SDK has one.
Install Java JDKs
Lets install Java 1.8 SDK from Oracle. Head over to the downloads page. Or you can download from Adobe's Software Distribution. Either way, you will need credentials to log in. Once downloaded, run the installer.
Whichever way you have installed them, you should now have two JDK's installed. To confirm it, run
You have the JDKs installed. However you need to manage the value of JAVA_HOME. You can do this with jEnv. Install it via a Homebrew formula
This only installs the executable. There are additional shell configs you must setup. This is where I rely on on Antigen to load the Oh My ZSH! plugin. Add the following lines to .zshrc
And restart your terminal. Now you need to add each JDK that was listed out by /usr/libexec/java_home -V. For example
And enable the following jEnv plugins
Now you can list the versions managed by jEnv
If you are using Homebrew it is simple. The Homebrew formula has a dependency on an OpenJDK. Since we are providing our own, let us going to ignore it.
And now run
Setting The Java Version
Now that you have installed the JDKs, Maven and jEnv, you are ready to start changing the Java version. There are 3 ways to do this using one of the version tokens listed with jenv versions.
Set the global version. This will be the version set by default whenever you open a shell.
Set the local version. This creates a file named .java-version in the current directory. This can be saved to source control.
Set the shell version. This only applies to the current shell instance.
jEnv is now managing the value of JAVA_HOME. To verify run
Which Maven will pick up on