Where is the path for the java command defined in Ubuntu?

I am struggling to get my Ubuntu 20.04 to get to recognize the correct java version. I have set the correct environmental variable inside a shell script in /etc/profile.d/

My $JAVA_HOME output:

root@Ubuntu-2004-focal-64-minimal /etc/profile.d # echo $JAVA_HOME
/home/miw/java/jdk-15.0.2

.. this points to the correct java version i want to use! (15.0.2)

Furthermore i appended this home directory to my $PATH variable:

root@Ubuntu-2004-focal-64-minimal /etc/profile.d # echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/miw/java/jdk-15.0.2

My Maven correctly recognizes the version, since it uses the $JAVA_HOME variable to determine the version.

root@Ubuntu-2004-focal-64-minimal / # mvn -version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 15.0.2, vendor: Oracle Corporation, runtime: /home/miw/java/jdk-15.0.2

Now:My question is where inside the startup process or generally in which file the default java command directory is defined. I DO NOT WANT just a solution to my problem. (like some automated programs where i can switch between alternatives etc.)

The system somehow still recognizes the preinstalled java version located inside /usr/lib/jvm/java-11-openjdk-amd64/bin as its default... and indeed if i take a look inside the /usr/bin directory there is a symlink called java which points to the /etc/alternatives/java symlink that points to /usr/lib/jvm/java-11-openjdk-amd64/bin.

My next guess was that this is due to the $PATH hierarchy since /usr/bin is defined BEFORE my custom $JAVA_HOME.

Well.. i tried deleting those symlinks both, hoping that the system would finally reach my custom $JAVA_HOME directory in search of a java executable.... what happes now is that when i try to show the java version an error occurs:

root@Ubuntu-2004-focal-64-minimal /etc/alternatives # java -version
-bash: /usr/bin/java: No such file or directory

Well of course its not there anymore because i have deleted it... Why does the system take the /usr/bin/java for granted anyway ? Where is this exact path for the java command defined ?

I verified this by creating my own symlink inside /usr/bin that points to my custom directory.. and tada..

root@Ubuntu-2004-focal-64-minimal /etc/alternatives # ln -s /home/miw/java/jdk-15.0.2/bin/java /usr/bin/java
root@Ubuntu-2004-focal-64-minimal /etc/alternatives # java -version
openjdk version "15.0.2" 2021-01-19
OpenJDK Runtime Environment (build 15.0.2+7-27)
OpenJDK 64-Bit Server VM (build 15.0.2+7-27, mixed mode, sharing)

So can anybody explain to me what actually happened here ? Why is the system assuming that /usr/bin/java MUST be where the java command is found ? This somehow destroys the purpose of the $PATH variable in the first place ! What am i not getting here ?

1 Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like