Manage multiple Java environment
This blog shows you how to install and manage multiple Java environments in your Linux or macOS using jenv
tool.
jEnv
is a command line tool to help you forget how to set the JAVA_HOME
environment variable.
Installation
Setting up jEnv
is pretty simple.
Linux
$ git clone https://github.com/gcuisinier/jenv.git ~/.jenv
MacOS using Homebrew
$ brew install jenv
Setup jEnv profile
Follow below steps to configure jEnv profile as per your Shell.
Bash shell
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile
ZSH shell
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(jenv init -)"' >> ~/.zshrc
$ source ~/.zshrc
Install & Configure JDKs
Now you can install multiple JDKs like openjdk-8
, openjdk-9
, oraclejdk-8
, oraclejdk9
, AdoptOpenJDK-8
, AdoptOpenJDK-9
etc. And add them into jEnv
.
Installing JDK
Following are the blogs, which will help you in installing different JDK on verity of OS.
Installing OpenJDK on Linux
Installing OpenJDK on Mac
Installing OpenJDK on Windows
Installing AdoptOpenJDK on Linux
Installing AdoptOpenJDK on Mac
Installing AdoptOpenJDK on Windows
Installing OracleJDK on Linux
Installing OracleJDK on Mac
Installing OracleJDK on Windows
In Linux distribution JDK get install in different path based on distribution and installation method. And in MacOS JDK get install in /Library/Java/JavaVirtualMachines/
.
Add JDKs into jEnv
Lets add JDK installation path into jEnv
, so that it can manage it.
MacOS
$ jenv add /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home
Ubuntu OS
$ jenv add /usr/lib/jvm/java-8-openjdk-amd64/
$ jenv add /usr/lib/jvm/java-11-openjdk-amd64/
Jenv versions
List Java versions maintained by jEnv tool.
$ jenv versions
Switch to local Jenv version
$ jenv local 12.0.1
Switch to global Jenv version
$ jenv global 12.0.1
Tips:
Enable following plugins if you are using Maven.
$ jenv enable-plugin maven
$ jenv enable-plugin export