Ubuntu
Execute the following command to install the default Java Runtime Environment (JRE), which will install the JRE from OpenJDK 11:
1 | $ apt install default-jre |
The JRE will allow you to run almost all Java software.
Verify the installation with:
1 | $ java -version |
You’ll see the following output:
1 | Outputopenjdk version "11.0.7" 2020-04-14 |
You may need the Java Development Kit (JDK) in addition to the JRE in order to compile and run some specific Java-based software. To install the JDK, execute the following command, which will also install the JRE:
1 | $ sudo apt install default-jdk |
Verify that the JDK is installed by checking the version of javac
, the Java compiler:
1 | $ javac -version |
You’ll see the following output:
1 | Outputjavac 11.0.7 |
Setting the JAVA_HOME
Environment Variable
Many programs written using Java use the JAVA_HOME
environment variable to determine the Java installation location.
To set this environment variable, first determine where Java is installed. Use the update-alternatives
command:
1 | sudo update-alternatives --config java |
This command shows each installation of Java along with its installation path:
1 | OutputThere are 2 choices for the alternative java (providing /usr/bin/java). |
In this case the installation paths are as follows:
- OpenJDK 11 is located at
/usr/lib/jvm/java-11-openjdk-amd64/bin/java.
- Oracle Java is located at
/usr/lib/jvm/java-11-oracle/jre/bin/java
.
Copy the path from your preferred installation. Then open /etc/environment
using nano
or your favorite text editor:
1 | sudo nano /etc/environment |
At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path, but do not include the /bin
portion of the path:
/etc/environment
1 | JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64/bin/java" |
Modifying this file will set the JAVA_HOME
path for all users on your system.
Save the file and exit the editor.
Now reload this file to apply the changes to your current session:
1 | source /etc/environment |
Verify that the environment variable is set:
1 | echo $JAVA_HOME |
You’ll see the path you just set:
1 | Output/usr/lib/jvm/java-11-openjdk-amd64 |
Other users will need to execute the command source /etc/environment
or log out and log back in to apply this setting.