I'm trying to find my java location within my Linux system and got this

[980@b449 ~]$ which java

[980@b449 ~]$ readlink -f $(which java)

what is the difference between the 2 commands?

3 Answers 3


which 2 commands? /usr/bin/java is a soft (symbolic) link to /usr/lib/jvm/java-1.6.0-openjdk-
There is no difference as they are the same file.
If you type something like

ls -l /usr/bin/java

You might get a result such as:

lrwxrwxrwx. 1 root root 22 Aug  5 17:01 /usr/bin/java -> /etc/alternatives/java

Which would mean you can have several java versions on your system and use alternatives to change the default one. Otherwise you can simply add and remove links to change the default one manually.

To create symbolic links use the command

ln -s /usr/lib/jvm/java-1.6.0-openjdk- /usr/bin/java

Or in general form

ln -s <original file> <link to file>

And use rm to delete the link as you would delete any other file.

  • remove existing symlink if any to /usr/bin/java. Using 'sudo rm -rf /usr/bin/java'. Then recreate a new one. Jul 1, 2023 at 3:36

readlink -f will:

canonicalize a path by following every symlink in every component of the given name recursively; all but the last component must exist

which will search:

for an executable or script in the directories listed in the environment variable PATH using the same algorithm as bash(1)

which doesn't care whether what it finds is a symlink or not: just that it's executable. It guarantees that the path it prints will always be inside one of the directories in PATH.

On your system, /usr/bin/java is a symlink to /usr/lib/jvm/java-1.6.0-openjdk- When you combine the two commands together like this you substitute the output of which into the command line of readlink -f to create:

readlink -f /usr/bin/java

That is, which has found where the first executable file called java is in your PATH, and the shell has inserted that path as an argument to readlink -f. readlink then looks up the path and finds that it's a symbolic link, and so it resolves that link (and any others it finds) to produce a complete direct path to the actual file itself.

For almost all purposes, these paths will be interchangeable to you - the symlink java will be automatically resolved to the real path when you use it, and modifications to the file itself will be made by your package manager, rather than you, so you never have to see it. You could run the program from either path, or with just java, and the result would be exactly the same, because it's the same actual executable that runs in the end.

The package manager will be using a symlink rather than putting the actual file inside /usr/bin because the JRE has a whole set of files it likes to have next to each other in unusual configurations, and a symlink lets the package manager present a normal-looking arrangement to you as the user. There will be many other files inside the /usr/lib/jvm/java-1.6.0-openjdk- that you'd never have any reason to deal with, and that don't participate in the system's ordinary library arrangements.


This single line command:

which java | xargs readlink -f

will do the job for you.

  • which java gives you i.e.: /usr/bin/java
  • readlink -f goes through all symbolic links /usr/bin/java -> /etc/alternatives/java -> /usr/lib/jvm/java-8-oracle/jre/bin/java and returns last one.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .