¡Hola!
Happy 2021! ✨ It’s already been a month and a half, but it still counts, right? 😊
When you are with Java, many circumstances may drive the need to work with its different versions. For example:
- You may still have some projects that require Java 8.
- You may have some projects that have been migrated to the latest Java LTS (as of this writing): Java 11.
- You may be staying up to date and have some projects that use Java 15.
I will share the setup I use on macOS to switch between Java Versions per project in 3 steps:
- Installation
- Manually switching Java versions
- Automagically switching Java versions
Installation
To install different Java versions I use Homebrew and these are the steps:
-
Update the formulae and Homebrew itself:
brew update
-
Look for the Java Version you wish to install, I use three commands:
|
|
The following, that is a more extensive search and where you’ll see more results:
|
|
You can also refine your search more once you get some names of formulae, for example:
|
|
will give you many more specific results.
- Choose the formulae, or the cask to install. I like to some information before installing using the following command:
|
|
For example for:
|
|
the output would be:
|
|
- Install the formulae. For example, the following command would install OpenJDK 15:
|
|
And the following would install OpenJDK 11
|
|
Manually switching between versions
Now that you have different versions installed, a quick and easy method to manually switch between versions is to:
- Export different
JAVA_HOME_X
env vars in the.bash_profile
file of your home directory, per Java version. For example:
|
|
- Add aliases to set the
JAVA_HOME
env var to one of the variables that you set before. For example:
|
|
You can then set a default Java version by adding for example: java8
to the .bash_profile
file. When you want to switch to a different Java version, all you have to do is call one of the aliases you defined. For example, given that you set java8
to be the default, when we run:
|
|
the output would be:
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
Then, when we want to switch to Java 15, we run:
|
|
and when we run:
|
|
The output would be:
openjdk version "15.0.2" 2021-01-19
OpenJDK Runtime Environment AdoptOpenJDK (build 15.0.2+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 15.0.2+7, mixed mode, sharing)
Automagically switching between versions
To have the Java version automagically switched between projects, I have used jEnv. From their website:
jEnv is a command line tool to help you forget how to set the JAVA_HOME environment variable
The first step then, would be to install jEnv on your machine. You can follow the instructions on their website. For macOS, you can install jEnv via Homebrew:
|
|
Then, add the following to your .bashrc
file to initialize jEnv:
|
|
Next, we need to tell jEnv were to find the different JDKs by using jenv add
. For macOS, the following folder includes the JDKs installed on your machine: /Library/Java/JavaVirtualMachines
. So, for JDK 15, we would run:
|
|
And the output would be:
|
|
Then, we can add JDK 8:
|
|
When we run:
|
|
the output is:
|
|
Then, go into the root directory of one of your projects and run:
|
|
For example:
|
|
This will create a .java-version
to the root directory of your project with 1.8
as the content.
When switching projects, jEnv will take care of switching versions 🎉
So, when you go to the terminal and change directory to the root folder of your project and run:
|
|
the output would be:
|
|
And, if we were to change the content of the .java-version
file and replace 1.8
with 15
, and then go to the terminal again and run:
|
|
the output would be:
|
|
Magic ✨
One more thing
I personally prefer to switch versions manually 😊, I check the Java version to use in the pom.xml
file of the project. It helps me set my mind into the Java version I will be using and become more conscious about it. I guess this may be an issue if there are many, many projects to manage, but until now that has not been an issue for me, so I still use the manual method ☕.
Well, I hope this helps!
May the Force be with you, always! ✨