Configuring Java
You can configure which version is the default for use in the command line by using update-alternatives
, which manages which symbolic links are used for different commands.
sudo update-alternatives --config java
The output will look something like the following.
There are 5 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 auto mode
1 /usr/lib/jvm/java-6-oracle/jre/bin/java 1 manual mode
2 /usr/lib/jvm/java-7-oracle/jre/bin/java 2 manual mode
3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
4 /usr/lib/jvm/java-8-oracle/jre/bin/java 3 manual mode
5 /usr/lib/jvm/java-9-oracle/bin/java 4 manual mode
Press <enter> to keep the current choice[*], or type selection number:
You can now choose the number to use as a default. This can also be done for other Java commands, such as the compiler (javac
), the documentation generator (javadoc
), the JAR signing tool (jarsigner
), and more. You can use the following command, filling in the command you want to customize.
sudo update-alternatives --config command
Setting the JAVA_HOME
Environment Variable
Many programs, such as Java servers, use the JAVA_HOME environment variable to determine the Java installation location.
Copy the path from your preferred installation and then open /etc/environment using Sublime Text or your favourite text editor.
sudo subl /etc/environment
At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path.
JAVA_HOME="/usr/lib/jvm/java-8-oracle"
Save and exit the file, and reload it: source /etc/environment
.
You can now test whether the environment variable has been set by executing the following command: echo $JAVA_HOME
. This will return the path you just set.
update-java-alternatives
wasn't available for me. I just replaced that command withupdate-alternatives java
. – nofinator Nov 07 '18 at 21:44java
executable. Which Ubuntu version do you use? – danzel Nov 13 '18 at 08:20JAVA_HOME
you have to run the following command:
– janb Nov 16 '18 at 10:23source /etc/environment
JAVA_HOME
in/etc/environment
. I just switched back and forth between java 8 and 11, and there is still noJAVA_HOME
in/etc/environment
. In fact, it is set nowhere in my environment. I therefor rejected the suggested edit. If there are circumstances in which the suggested command is needed, please suggest a new edit and explain when and why it would be necessary. – danzel Jun 10 '20 at 14:24$JAVA_HOME
is unaffected by any of the proposed commands, too. My~/.bashrc
has a lineexport JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:bin/javac::")
though and that does the trick for me:source ~/.bashrc
. If your~/.bashrc
doesn't have that line, run directlyexport JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:bin/javac::")
. – Giszmo Jun 18 '20 at 14:27