What is command to see all java versions installed on linux?

41

8

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.

wheaties

Posted 2013-03-21T21:15:36.530

Reputation: 513

Answers

55

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.

mvp

Posted 2013-03-21T21:15:36.530

Reputation: 3 705

3Any reason not to use update-alternatives --list java just to list them? – Xiao – 2015-10-17T01:20:59.067

4@Xiao: unfortunately, not all versions of update-alternatives have --list option. e.g. Centos 6.7 doesn't. – mvp – 2015-10-19T17:45:20.097

1In RHEL 7, --list exists, but doesn't take any other parameters, so you have to | grep java – Dmitri – 2016-08-17T18:33:35.037

4

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.

Brad Patton

Posted 2013-03-21T21:15:36.530

Reputation: 9 939

@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 – 2019-04-10T16:42:41.743

@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 – 2019-04-10T20:51:15.770

3"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 – 2013-03-21T22:22:00.280

2

I use this to list the Java installs available:

sudo update-alternatives --display java

Jaime C Vega

Posted 2013-03-21T21:15:36.530

Reputation: 21

bash: update-alternatives: command not found ? – Jimmy Obonyo Abor – 2016-12-02T21:56:37.300

1

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.

MorganGalpin

Posted 2013-03-21T21:15:36.530

Reputation: 111

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

maedox

Posted 2013-03-21T21:15:36.530

Reputation: 11