Using Jenv
April 06, 2020
Jenv is a command line tool, which you can use to conviniently switch between various version of java. I have used this to check if my Java 8 code base will it work on Java 11.
Basic installation steps are provided [here] https://www.jenv.be/
Checkout the additional configurations here https://github.com/jenv/jenv
At a high level these are the steps to follow to setup for the first time. If you already have it setup jump to switching versions section.
Setup
- Run
git clone https://github.com/gcuisinier/jenv.git ~/.jenv
to install jenv -
Run
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
to add the jenv bin dir to your path.- NOTE: In linux
echo 'PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
- NOTE: In linux
- Run
echo 'eval "$(jenv init -)"' >> ~/.bash_profile
to initialize jenv on startup - Run
mkdir -p ~/.jenv/versions
to create jenv’s home dirs (since it doesn’t do it automatically) -
Run
jenv add <path-to-jdk>
to add the JDK you installed earlier to jenv.- NOTE: See which version is currently being used in the shell
java -version
- To locate the executable
which java
- To find the actual path
readlink -f <executable path>
- NOTE: In mac os
jenv add /Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home
This might not be the exact path of your JDK. - You can use autocomplete to get to the correct dir
- Just start with
/Library/Java/JavaVirtualMachines
and start tabbing - Make sure your dir ends with
Contents/Home
if you are on Mac OS - NOTE: In linux
jenv add /usr/lib/jvm/java-11-openjdk-amd64
- NOTE: If you recieve an error, use the command
source ~/.bash_profile
or close the shell and open again.
- NOTE: See which version is currently being used in the shell
-
Run
jenv global openjdk-11.0.2.jdk
to configure your global jdk version- NOTE: Might not be the exact version. Check the output of your
jenv versions
- NOTE: Might not be the exact version. Check the output of your
- Run
jenv enable-plugin export
to allow jenv to set your $JAVA_HOME, then create a new terminal session
Adding new version of Java
- Download the java tarball from https://jdk.java.net/archive
- Untar the file and move it to desired location.
-
Run
jenv add <location>
- NOTE: In linux, download and extract JDK 14.
curl https://download.java.net/java/GA/jdk14/076bab302c7b4508975440c56f6cc26a/36/GPL/openjdk-14_linux-x64_bin.tar.gz | sudo tar -xzvf - --directory /usr/lib/jvm
- Add to jenv
jenv add /usr/lib/jvm/jdk-14
Switching between versions in shell
jenv versions
jenv shell <desired version>
java -version
Other tips
See which version is currently being used in the shell java -version
To locate the executable which java
To find the actual path readlink -f <executable path>
If you are using java set by jenv, to locate executable run jenv which java
.
Use this path in the readlink command to find jdk installation location.