4

Is there a way to detect the installed java on various different Linux distributions? With Windows you could use the JAVA_HOME or the registry for that, but Linux? Can it be also detected if this is JDK, JRE, 32 or 64 bit?

REPHRASE: If I need 64-bit JDK on Linux, how do I programatically check that it is present and inform the user that he has some other java instead?

m_pGladiator
  • 8,462
  • 7
  • 43
  • 61
  • 1
    *"the installed Java on Linux"* [sic] doesn't mean much. You're not forced to be root to install Java on Linux and hence every user can install as many JRE (or JDK) as he want in his account. Switching from one JRE to another is as simple as doing: *export PATH=~/jdk1.6/bin:$PATH*. So you can have one user having, say, a 1.6 32-bit JVM and another having a 1.7 64-bit JVM. On correctly configured systems it's impossible to know which Java is installed in another user account. I'd rephrase: *"Is there an uniform way to detect which Java version is available from a user account?"* – TacticalCoder Oct 24 '11 at 14:23
  • Look through the System & Environment tabs of the code shown on [this answer](http://stackoverflow.com/questions/7585699/list-of-useful-environment-settings-in-java/7616206#7616206) to see if any of them fit your need. – Andrew Thompson Oct 24 '11 at 17:01

5 Answers5

5

You can always use java -version.

This works on all platforms, but make sure that Java is included in the system PATH variable.

Mahmoud Hanafy
  • 7,958
  • 12
  • 47
  • 62
1

No unified way. You can use JAVA_HOME env. variable but it is not defined on all machines. But there is a "common" way like ls /usr/java/*/bin/java | tail -1.

This should give in most cases you the latest version.

AlexR
  • 114,158
  • 16
  • 130
  • 208
1

To see the file called by the java command use which java, although this will often be just a symlink to the real executable, such as /usr/bin/java. This works across all distros as far as I know.

On some Linux distros you can use update-alternatives --display java (possibly with sudo) to see a list of all the java executables installed on your system, and it will also tell you which one the symlink points to. You can switch between them by using the --config option instead.

andronikus
  • 4,125
  • 2
  • 29
  • 46
  • update-alternatives is not present on all Linux distributions. It is not available on OpenSuse, to take one example that I just happened to have availbale right now. – Hannes Ovrén Oct 24 '11 at 13:56
  • Ah, indeed. I'm on Ubuntu so I do have it. I'll edit my answer to reflect that. Thanks! – andronikus Oct 24 '11 at 14:02
1

You can use java -version in the terminal to find out some info.

andronikus
  • 4,125
  • 2
  • 29
  • 46
Miggs
  • 11
  • 1
  • 3
0

There are many answers here helping you to check if Java is installed. If this is the case you can get more information programatically. SystemUtils of commons-lang gives you many information about the Java version actually running.

Thor
  • 6,607
  • 13
  • 62
  • 96