Have you had problems with several versions of Java on your machine? Do you lose time configuring the environment variables? There is an elegant solution in the Linux world and it is called jEnv.
Motivation
You need to maintain your Java programs written in Java 8 or even lower. Develop applications with Java 11 and still try Java 12 or 13. Certainly you will have problems.
Scenario
Assuming you already have a development environment with Java 8 and Maven. In the examples, I'll be using Linux Mint.
It is common to have problems when we have more than one version of Java on the same machine. To solve this, we end up doing several scripts to change JAVA_HOME, using the alternatives framework and we often need to restart the user session for the changes to take effect.
Installing OpenJDK 11 with JDK 8
The idea is to keep the current version of your Java, install JDK 11 and run one version or another as the project needs. We'll see how easy it is to manage this with some tools. When I refer to Java 11, JDK or OpenJDK, I will always be referring to OpenJDK 11.
Download OpenJDK:
$ wget https://download.java.net/java/GA/jdk11/13/GPL/openjdk-11.0.1_linux-x64_bin.tar.gz
$ sudo tar xvf openjdk-11.0.1_linux-x64_bin.tar.gz --directory /usr/lib/jvm/
After that, if you run the command java -version
you will see that nothing has changed and we continue with Java 8:
$ java -version
java version "1.8.0_191" Java(TM) SE Runtime Environment (build 1.8.0_191-b12)Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
Maven is still there with the current Java:
$ mvn -version
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T15:33:14-03:00) Maven home: /opt/maven Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-8-oracle/jre
To check if Java 11 is ok, just run the command:
$ /usr/lib/jvm/jdk-11.0.1/bin/java -version
openjdk version "11.0.1" 2018-10-16 OpenJDK Runtime Environment 18.9 (build 11.0.1+13) OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)
Ready! We have both versions of Java on the same machine, now what?
Assuming most of our projects will be with Java 8 for some time, let's define this version as "global" and Java 11 as the "local" version.
jEnv
jEnv is a command-line tool to help you forget how to set the JAVA_HOME environment variable. It is a tool that allows you to configure Java versions in a global or local way.
Assuming we already have Java 8 installed and configured using it with maven for example.
Installing jEnv:
This tool is only available for Linux and Mac. To install on Linux, follow the steps below (assuming you have git installed):
$ git clone https://github.com/gcuisinier/jenv.git ~/.jenv
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile
If you use Zsh:
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(jenv init -)"' >> ~/.zshrc
Close the terminal and open it again. Run the jenv
command
If everything is ok, you should see the message jenv
+ version
. Ex: jenv 0.5.1-5-gaf89d78
If it does not work, perform step 2, for bashrc instead of bash_profile
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(jenv init -)"' >> ~/.bashrc
Adding Java versions to jEnv:
Java 8:
$ jenv add /usr/lib/jvm/java-8-oracle
Java 11:
$ jenv add /usr/lib/jvm/jdk-11.0.1
Check java versions:
$ jenv versions
system
11.0
11.0.1 (set by /home/sandro/.jenv/version)
1.8
1.8.0.191
Running
Assuming you need to keep Java 8 as default for legacy applications and use Java 11 in certain projects or directories.
Defining a global version:
$ jenv global 1.8.0.191
Defining the local version (per directory):
$ jenv local 11.0.1
Conclusion
We install Java 11 without compromising the environment that is already working with Java 8. We install and configure the jEnv tool, generate and compile a Java 11 project. In the next post, we will analyze a Java 8 application and see if we can run it in Java 11.
If you have any questions or problems, leave a comment. Thanks for reading.
Top comments (2)
Nice. I never heard of this tool. I use SDK for this. But I also use it for maven, sbt, scala, groovy, etc.
Other cool option is ASDF asdf-vm.com/#/