75

I know about java -version. I don't care what version I'm currently running. I care what other versions are installed on my linux box. If it's another java -* command I didn't see it in the java -help.

I've tried googling it but the answers are either for Windows or they say "use java -version." I know I've done this before.

5 Answers 5

89

On most Linux distributions you can use update-alternatives like this:

sudo update-alternatives --config java

It will list all packages that provide java command and will let you change it. If you don't want to change it, simply Ctrl-C from it.

There is only one catch - if you installed some java not using official package manager (dpkg/apt-get, rpm/yum), but simply extracted it, update-alternatives will not show it.

3
  • 4
    Any reason not to use update-alternatives --list java just to list them?
    – Xiao
    Oct 17, 2015 at 1:20
  • 5
    @Xiao: unfortunately, not all versions of update-alternatives have --list option. e.g. Centos 6.7 doesn't.
    – mvp
    Oct 19, 2015 at 17:45
  • 2
    In RHEL 7, --list exists, but doesn't take any other parameters, so you have to | grep java
    – Dmitri
    Aug 17, 2016 at 18:33
9

You could do:

find / -name java 

To find all files. The package manager with your version of Linux should also be able to list them.

3
  • 5
    "package manager" ... Unless someone installed a java version manually. Find is a valid approach, though :) find / -type f -name java -print 2>/dev/null | xargs -i echo {} -version | bash
    – tink
    Mar 21, 2013 at 22:22
  • @tink How would one use the find with xargs but add a blank line between each version result when there are multiple 'java' executables?
    – Lee Meador
    Apr 10, 2019 at 16:42
  • 1
    @LeeMeador find /usr -type f -name java -print 2>/dev/null | xargs -i echo {} -version | bash 2>&1 | sed ':a;N;$!ba;s/\n/\n\n/g'
    – tink
    Apr 10, 2019 at 20:51
6

I use this to list the Java installs available:

sudo update-alternatives --display java
1
  • bash: update-alternatives: command not found ? Dec 2, 2016 at 21:56
3

I was previously using the following to determine the java 8 installation for an application that needed an environment variable set so it could use a java version that was not set as the default:

update-java-alternatives -l java-8-oracle

However, that stopped working today. The update-java-alternatives script/program is no longer installed on my Ubuntu 14.04 system. What's installed now is alternatives.

What I use now to get a specific alternatives java path is:

alternatives --display java | grep priority | grep jdk-1.8

Then I can massage the result to get what I need for my app's environment variable.

1

You leave a lot to be desired as far as details about your setup goes. Java can be installed in different ways in linux. You can install via your distributions package maanger, like apt, yum, yast, or you could install it manually.

How ever you installed it a Java installation needs the java executable to do any good in most cases, so you could use the locate or find commands to find the different ones.

Example which will most likely find links and duplicates, but the directory names should help you pinpoint it:

for f in $(locate -ber '^java$'); do test -x && echo "$f"; done

You must log in to answer this question.

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