Installing Java: Managing Multiple JDK Versions Easily with asdf-java
Java installations need to be smart. More specifically, it should be installed with the future in mind.
If you develop in Java, you will inevitably need to use multiple versions or distributions of the Java environment. Different platforms, different packages, and different versions support the same Java language. Different distributions also have different licensing, performance, and technical support, so you need to use the right one for your particular needs.
1. asdf-java for a smart Java installation
Installing Java with the default installer from the vendors that provide Java distributions, including Oracle, can be cumbersome because each has a different installation path. The same goes for installing multiple versions of the same distribution. So there are a number of free version managers that allow you to manage multiple versions and distributions of Java in one place. One I use and recommend is asdf-java.
asdf is responsible for versioning not only Java, but also many other languages and development tools. You can install plugins for asdf-java, asdf-python, or any other language or tool and manage them all with one program. Even if you don't use other languages, the simplicity of asdf makes it worth using.
For a more detailed description of asdf and how to install it, please refer to the asdf installation and usage post, but for now I'll assume you have asdf installed on your computer.
Installing the asdf-java plugin and installing Java with asdf works the same on MacOS and Windows. I'll use macOS.
2. To install the asdf-java plugin
Open a terminal and run the following command to install the asdf-java plugin.
asdf plugin-add java https://github.com/halcyon/asdf-java.git
If you don't get an error message and proceed to the next prompt, the installation went well.
Run the result:
To verify the installation, type the following command. This command prints a list of all Java distributions that are currently available for installation.
asdf list all java
I've appended a condition and returned a list that supports JDK 20
, but there are some exceptions.
Result:
If you see something like this, you've successfully installed the plugin.
If you don't know which of the various Java distributions to install, the following article will help you.
6 Common Java Distributions: Oracle, OpenJDK, Adoptium, Zulu, Corretto, Liberica
3. How to install your Java version
Now let's install the Java version you want. Use the command below to see all the versions available for installation in your distribution.
In the [distribution_name]
part, type the name of the distribution you want to install. I'm going to install the OpenJDK distribution.
asdf list all java [distribution_name]
You can see the output as shown below.
Result of the execution:
Now let's install it, which is done by running the following command. In the [version_name]
part, please put the same version name as printed above.
asdf install java [version_name]
The terminal will display a loading bar and the installation will continue. Once the installation is complete, you can check the installed version with the following command.
asdf list java
The results are as follows
Result of execution:
4. Verify the Java installation path
The path to Java installed by asdf is ~/.asdf/installs/java/[installed_version]
.
If you are using WSL on Windows, the directory is the same. You can easily check it with the command
5. Setting the Installed Java Version
The next step is to set up your installed Java version for use. Like most version managers, ASDF allows you to set three usage scopes.
- Use in the current shell
- Use in the current directory
- Use everywhere in this computer
The commands for each are shown below. The commands are intuitive and easy to learn.
asdf shell java [version_name]
asdf local java [version_name]
asdf global java [version_name]
5.1. Setting the Java version in the current shell
Let's enable it in the current shell and check it out. Type java --version
and see if it's the version you want.
Result:
After you exit the terminal, run it again and the settings you made before will be initialized.
5.2. Setting the Java Version in a Specific Directory
If you use different versions of Java in different projects, it's most convenient to set a specific version in a directory. This is because if we put it in the directory, the computer will automatically detect and run the set version. Let's show you how to do this in a nutshell.
I simply created 2 folders with java files. I set one to openjdk and the other to the zulu distribution. If you go into each folder and print out the java version, you can see that they are set to different versions, as shown below.
If you navigate to a different directory, you will see that no Java version is set there.
5.3. Setting the Java version across machines
Finally, let's go through the process of setting and changing the Java version used on all computers.
As shown above, you can change it to any version you have installed. Also, since it is set globally, it will remain set even if you restart the terminal or reboot your computer.
6. How to use with IntelliJ
The Java version installed by asdf is automatically detected by most IDEs, including IntelliJ. Therefore, you can easily change it to the appropriate version in the preferences window.
7. Conclusion
We've seen how to manage multiple versions of Java with ASDF. There are many languages that claim to be replacements for Java, but in every industry, whether it's servers, Android, or big data, Java is one of the most popular languages for both commercial and open source projects.
It's a language with many more distributions and versions than other languages, which can be daunting for some developers. But it's also a language that helps developers choose the best runtime for their services. Consider the options available to you, do some testing with the help of ASDF, and you'll find yourself a better Java developer.
