Score:0

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

si flag

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 ?

hr flag
*"Why does the system take the /usr/bin/java for granted anyway"* probably the location was just cached by your shell - see for example [Pytest is in PATH but not found](https://askubuntu.com/a/861386/178692)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.