Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

[980@b449 ~]$ which java
/usr/bin/java


[980@b449 ~]$ readlink -f $(which java)
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java

what is the difference between the 2 commands?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

which 2 commands? /usr/bin/java is a soft (symbolic) link to /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java
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-1.6.0.0.x86_64/jre/bin/java /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.

share|improve this answer

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-1.6.0.0.x86_64/jre/bin/java. 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-1.6.0.0.x86_64 that you'd never have any reason to deal with, and that don't participate in the system's ordinary library arrangements.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.