Switching JDKs using jEnv
If you already installed latest version of java such as 12 and you check the version of java, it still points to java 12 and not 11. However you can switch between different installed JDKs by setting JAVA_HOME environment variable and having the $JAVA_HOME/bin in $PATH environment variable.
You can also use a command line tool called jEnv to switch between different version of jdks.
What’s jEnv ?
https://www.jenv.be/
jEnv is a command line tool to help you forget how to set the JAVA_HOME environment variable
Mac OS X via Homebrew
To install jEnv on mac using brew use following command:
$ brew install jenv Updated 2 taps (homebrew/cask and caskroom/cask). No changes to formulae. ==> Downloading https://github.com/jenv/jenv/archive/0.5.2.tar.gz ==> Downloading from https://codeload.github.com/jenv/jenv/tar.gz/0.5.2 ################################################################## 100.0% ==> Caveats To activate jenv, add the following to your ~/.zshrc: export PATH="$HOME/.jenv/bin:$PATH" eval "$(jenv init -)" ==> Summary ? /usr/local/Cellar/jenv/0.5.2: 80 files, 70KB, built in 2 seconds
Referring to Caveats section in above installation logs for jEnv, you will see something like following notes:
==> Caveats To activate jenv, add the following to your ~/.zshrc: export PATH="$HOME/.jenv/bin:$PATH" eval "$(jenv init -)"
Linux/Unix/OS X Installation by source code
First you need to install git on your Linux/Unix/OS X machine. Click here for more information on Working with git cli tool.
If you have git cli command installed, clone jEnd github repository using following following command into ~/.jenv folder
$ git clone https://github.com/gcuisinier/jenv.git ~/.jenv
Post jEnv Configurations
If you are using bash or zsh you need to go and add following lines into their profile ~/.bash_profile or ~/.zshrc accordingly
Bash
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile $ echo 'eval "$(jenv init -)"' >> ~/.bash_profile
Zsh
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc $ echo 'eval "$(jenv init -)"' >> ~/.zshrc
Exit the ssh session or logout the user session you are in and login again or you can simply reboot your OS.
Validating the Installation
If everything is good, you should be able to see jenv version and cli help by running just jenv command like this:
$ jenv jenv 0.5.2-4-gd8ffd5b Usage: jenv [] Some useful jenv commands are: commands List all available jenv commands local Set or show the local application-specific Java version global Set or show the global Java version shell Set or show the shell-specific Java version rehash Rehash jenv shims (run this after installing executables) version Show the current Java version and its origin versions List all Java versions available to jenv which Display the full path to an executable whence List all Java versions that contain the given executable add Add JDK into jenv. A alias name will be generated by parsing "java -version" See `jenv help ' for information on a specific command. For full documentation, see: https://github.com/hikage/jenv#readme
Adding JDK path to jEnv
Here is where you can find installed jdks in you Mojave macOS
$ ls /Library/Java/JavaVirtualMachines/ jdk1.8.0_181.jdk jdk1.8.0_192.jdk openjdk-11.0.2.jdk openjdk-12.0.1.jdk
And here are some example where you can find the installed JDK on your Linux machine. Following paths are in my Fedora 30
Using find command you can search for installed openjdk for example like this.
$ find / -name 'openjdk' /var/lib/alternatives/jre_12_openjdk /var/lib/alternatives/jre_openjdk /etc/java/java-12-openjdk /etc/java/java-12-openjdk/java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64 /etc/alternatives/jre_12_openjdk /etc/alternatives/jre_openjdk /usr/lib/jvm/java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64 /usr/lib/jvm/jre-12-openjdk /usr/lib/jvm/jre-openjdk /usr/lib/jvm/jre-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64 /usr/share/man/man1/pack200-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz /usr/share/man/man1/java-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz /usr/share/man/man1/keytool-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz /usr/share/man/man1/jjs-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz /usr/share/man/man1/rmid-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz /usr/share/man/man1/unpack200-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz /usr/share/man/man1/rmiregistry-java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64.1.gz /usr/share/icons/hicolor/32x32/apps/java-12-openjdk.png /usr/share/icons/hicolor/48x48/apps/java-12-openjdk.png /usr/share/icons/hicolor/16x16/apps/java-12-openjdk.png /usr/share/icons/hicolor/24x24/apps/java-12-openjdk.png
You should find it somewhere inside /usr not /etc or /var folders. Also make sure you don’t want JRE you are looking for JDK path. In this example, here is the location, look it up in above find result.
/usr/lib/jvm/java-12-openjdk-12.0.1.12-1.rolling.fc30.x86_64
Using jenv add you can add jdk path to jenv environment
$ jenv add /Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home $ jenv add /Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home $ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
Using jenv versions you can see the list of java versions installed on our mac
$ jenv versions system (set by /Users/youruser/.jenv/version) 1.8 1.8.0.192 11.0 11.0.2 12.0 12.0.1 openjdk64-11.0.2 openjdk64-12.0.1 oracle64-1.8.0.192
Configure global version
You can set your jdk globally using following command.
$ jenv global 11.0
Make sure you open another shell after executing above command and check the version of java
$ java -version openjdk version "11.0.2" 2019-01-15 OpenJDK Runtime Environment 18.9 (build 11.0.2+9) OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
Configure local version (per directory)
Or you can set jdk for the current folder you are in using following command
~/java01$ jenv local 1.8
If you check java -version when you are inside java01 that you set jdk using jenv local, you will see the java version is 1.8
~/java01$ java -version java version "1.8.0_192" Java(TM) SE Runtime Environment (build 1.8.0_192-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)
And if you somewhere else it will be still version 11
~$ java -version openjdk version "11.0.2" 2019-01-15 OpenJDK Runtime Environment 18.9 (build 11.0.2+9) OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
Configure shell instance version
However you can specify the java version for specific shell window you opened. That means if you go to another window or close the shell that you set jdk version using jenv shell, it will go back and show the jdk gobal version
Here is the command to set shell instance version
$ jenv shell openjdk64-12.0.1
And the version will be 12
$ java -version openjdk version "12.0.1" 2019-04-16 OpenJDK Runtime Environment (build 12.0.1+12) OpenJDK 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)