Multiple Java Versions
If you use multiple versions of Java in your development environment, perhaps you wished there was a better way to switch from one to the other, instead of remembering to set the JAVA_HOME environment variable.

Forgot to set the JAVA_HOME
environment variable? If you use multiple versions of Java in your development environment, perhaps you wished there was a better way to switch from one to the other, instead of remembering to set the JAVA_HOME
environment variable.
Introducing jEnv:
jEnv is a command line tool to help you forget how to set the
JAVA_HOME
environment variable
Install
Here are the steps to install jEnv in your development environment.
Simply clone the jEnv repository from GitHub.
git clone https://github.com/gcuisinier/jenv.git ~/.jenv
You need Homebrew (package manager for Mac OS X).
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install jEnv using brew.
brew install jenv
TBD
Update your command line environment with jEnv specific configuration.
export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"
alias jenv_set_java_home='export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"'
Configure
Next you'll need to add the JAVA home directory paths to your jEnv environment. To list all the JVM versions and architectures in your environment, run:
/usr/libexec/java_home -V
You may get an output similar to (e.g. on Mac OS X):
Matching Java Virtual Machines (3):
11.0.1, x86_64: "OpenJDK 11.0.1" /Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home
1.8.0_192, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
1.8.0_101, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
Based on the JVM list above you may add the JAVA home directory paths to your jEnv environment, e.g.:
jenv add /Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
Now your jEnv environment is ready to manage the various JVM versions in your development environment.
jenv versions
The aforementioned command would yield an output similar to:
system
1.8
1.8.0.192
11.0
11.0.1
openjdk64-11.0.1
oracle64-1.8.0.192
You can set specific versions of JVM per workspace directory. Assuming you have a directory with a Java code base requiring JVM 1.8, you can switch to that directory and set the JVM version of choice, e.g.:
jenv local 1.8
You may still choose a default global
version of JVM on your system, e.g. 11
:
jenv global 11
Happy Java coding!