Assuming you use bash shell and installed Java with the Oracle installer, you could add the following to your
export JAVA_HOME=$(/usr/libexec/java_home) export PATH=$JAVA_HOME/jre/bin:$PATH
This would pick the correct
JAVA_HOME as defined by the Oracle installer and will set it first in your
$PATH making sure it is found.
Also, you don’t need to change it later when updating Java.
As per the comments:
Making it persistent after a reboot
Just add those lines in the shell configuration file. (Assuming it’s
.profile (for ubuntu)
Using a custom Java installation
JAVA_HOME to the root folder of the custom Java installation path without the