A Ready Solution for Unix
Lucky you if you have to use both Java 7 and 8 for different projects on the same dev box. I can totally relate the pain.
Initially, I was hoping brew would be able to magically sort out this problem for me. And boy, how naive was I. After few digging, it is not hard to find out brew is surely not the tool for this kind of job.
This is how jenv comes to the rescue. Obviously, jenv is a Java version managing tool. How jenv works is by setting up a shim in your home directory, and by adding the following lines to your .bash_profile , it then takes over and finds the right Java version to use globally or in a specific directory.
Of course, you need to have different Java versions installed, and this is something brew cask will happily handle.
The next important thing is to make sure jenv is aware of the JDKs installed by adding them to jenv . The Java versions are normally installed under /Library/Java/JavaVirtualMachines/ .
Using jenv is in fact really easy and pleasant with command line.
This seems like a happy ever after love story, but think again, is it though?
The likelihood is the project is now configured to use right Java version, but other tools, such as Maven, may disagree. The reason being as jenv shim takes over .bash_profile , it does not physically reconfigure Java version installed in /usr/bin . This will cause a hell lot of confusion while the project is configured to use one version of Java, yet Maven uses another version of Java by default.
There are different approaches to solving this problem, but rather than living with a tool I have no extended understanding of (which has in fact screwed me over more than once), I came up with a home grown solution.
A Home Grown Solution for Unix
Like I mentioned before, the downside of jenv is that the default version of Java remains unchanged, which causes lots of confusion.
To tackle this problem, my plan is to change default Java environment. The default Java executables configured in /usr/bin are nothing but symlinks, which means despite the fact that they should remain the same (if you want anything to work at all) on the surface, by pointing the executables to the version I want can archive the Java version switching just the same.
To make this work, first of all, you need to install different Java versions needed through our best friend brew cask . Then, replace all the current Java binaries in /usr/bin with the target version.
Do remember to check your Java version to confirm the great success.
For whoever is not confident of what they are doing, I’d encourage you to take a copy of existing configs before trashing them.
As for the lazy ones, you know I must have a script to do this kind of dirty work. Yes, you are right. But also bear in mind, I have only come so far to make the script work for switching between Java 7 and 8 on a Mac with the listed Java versions installed.
After going through my solution, I am sure everyone would be able to create a version tailored to their systems.
One last tip I would give is: make sure Java version to use is compatible with the tools. I have colleagues trying to configure Maven 3.2+ to use Java 1.6, and I just can’t stress enough how big a disaster that is.
A Piece of Cake in Linux
Comparing to Unix, switching Java versions (OpenJDK) on Ubuntu 14.04 is like walking in the park.
It is quite unlikely, but if for any odds the package is not available, try running sudo add-apt-repository ppa:openjdk-r/ppa first.
After all, the solution is solid and widely used by the community, so really, don’t try to be creative unless you are really bored.
How to Install Java on Ubuntu 18.04
This article will teach you how to install Java on Ubuntu 18.04. The commands will work on any private server or computer running the latest version, Ubuntu 18.04 Bionic Beaver, as well as 16.04, and any other Long Time Support (LTS) releases. Java is usually distributed in three editions, namely Micro (ME), Enterprise (EE), and Standard Edition (EE). For this guide, we’ll use Java SE 11 (LTS).
Overall, Java is one of the leading programming languages in the world. It’s created to have as little dependencies as possible, which allows application developers to apply the “write once, run anywhere” philosophy. As a result, a compiled Java code can operate on any platform which supports Java. You can utilize it to build anything from simple web applications to more advanced pieces of software.
Without further ado, let’s see how we can install Java on Ubuntu 18.04.
JDK or JRE?
The OpenJDK (Java Development Kit) and Oracle JRE (Java Runtime Environment) are the two main kits you’ll work with. Oracle Java contains the JVM, the Java program, alongside other infrastructures, while the JDK is a superset of JRE. The JDK is essentially an SDK (software development kit), as it gives you access to everything in JRE plus tools such as jdb, javadoc, and the compiler itself – javac.
If you’re not interested in compiling Java software, the JRE will do just fine. If this changes, you can update it later.
The following commands imply you are already using a root user, but in case you’re not, you should add sudo at the beginning of your commands.
Install Java on Ubuntu Using the Default Package Manager
The first method to install Java on Ubuntu is through the default packages. Begin the process by updating the current packages to the latest version:
Once it finishes, install the latest version of Java Runtime Environment (JRE) by executing this following command:
It is also possible to install the Java Development Kit (JDK) instead. It is required by specific software or used to compile Java programs. JDK includes JRE, therefore there’s no disadvantage if you choose this method instead.
That’s it, Java is ready for use on your Ubuntu system. You can double-check if it was properly set up with these commands:
The output will be similar to:
Install Java on Ubuntu via Oracle JDK
Alternatively, it is possible to install Java on Ubuntu using the official Oracle JDK. Begin by updating your current packages to the latest version:
For this example, we’ll use a third party library managed by WebUpd8. In order to implement it easier, install the following package first:
Next, get the Java Uprising PPA with the following command:
You might even want to run a quick update while you’re at it:
Finally, you can install Java on your Ubuntu system by executing the following command and following the on-screen instructions:
Then make sure to choose “Yes” using your keyboard to accept the license terms and your installation will begin.
That’s it! Java is successfully installed. You may verify it by checking the version with these two commands:
A similar output will appear:
Managing Java
A single server can have multiple Java installations. You can set the default version using the following command line:
The following output will appear:
Enter the number from the selection menu which you wish to use as the default one. You can also do this with other Java commands, such as:
Setting the Java Home Environment
Another useful thing to know is how to set the JAVA_HOME variable. Most applications require it to find the location of your Java installation. The previously used command can help you locate the Java installation path:
Once you’ve copied the installation path, you will need to edit the environment file located in the etc directory:
Add the following line at the end to set the JAVA_HOME value:
Important! Don’t forget to update it with the actual path to your Java installation.
Press CTRL+X to finish editing and save the changes. Next, make the changes by applying this command:
You can double-check if it’s active by entering:
Conclusion
Congratulations, you now know how to install Java on Ubuntu 18.04. You’ve also learned some basics, such as setting the default Java version and defining the JAVA_HOME variable.
If you found this article useful, feel free to share it with the world. If you have any tips, suggestions, or ideas of your own, we eagerly await them in the comment section below.
Learn What Else Ubuntu Can Do!
Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.
Switch Java versions on any systems you like
If you’re working on many Java projects, you will be in situation of switching Java version continuously. I face this too.
This is how to do it.
For Windows
Setup the basic JAVA_HOME environment variables
and then, create one or several batch files to switch version, which will enable the ability to change the JAVA_HOME variable just by one click.
For MacOS
The default location of Oracle Java is at,
where MAJOR, MINOR, MACRO, UPDATE are the corresponding numbers of Java version.
So to switch version, use the following command,
For Debian/Ubuntu -based Linux
Use the alternative configuration tool,
For RedHat/Fedora -like Linux
Use the alternative configuration tool, but first, you must become a superuser
Note: for the RedHat/Fedora -like distros, you need to run configuration for both java and javac.
For Manjaro/Arch Linux
Just install all JDK version you want, by picking from the list
Then see what are installed,
Get a name of version you want to use, then issue this command,
where JAVA-X-JDK is the name result from the status command.
How to switch between different java versions in Linux?
I manually installed Java 8 and installed Java 11 via command line (open-jdk). I’m a noob at linux and don’t know how to switch between the versions.
I ran the following commands to change the default version.
Returns:
Returns:
1 Answer 1
You can use SDKMan which allow us to do the same, SDKMan is a free to use open-sourced tool which helps us to install and manage parallel versions of around 29 Software Development Kits such as Java, Groovy, Scala, Kotlin and Ceylon. Ant, Gradle, Grails, Maven, SBT, Spark, Spring Boot, Vert.x.
And it also sets the _HOME and PATH environment variables automatically.
You can install SDKMan by running below commands
You can list all versions of Java using
And you can install different-different Java versions using sdk install command e.g.
And you can switch between them using sdk use and sdk default commands, You can read more about it on my blog How to Install Multiple Versions of Java on the Same Machine.