Why "which" command does not give you the correct path?

Solution 1:

which is an external program, which tries to determine how your shell will resolve a command from the existing $PATH, but it is possible for it to get it wrong.

If you're using bash as your shell, use the bash built-in command "type" to have the shell itself tell you how it will resolve that command:

$ which mvn
/usr/bin/mvn
$ type mvn
mvn is /usr/bin/mvn
$ type type
type is a shell builtin
$ type which
which is hashed (/usr/bin/which)

You can see here that in my environment that /usr/bin/which and the shell built-in type agree. In yours it seems they don't, though I am curious why they don't. Perhaps you have PATH changes that were not exported? Try "export PATH" and then "which mvn" again.