1

I have a linux box (Ubuntu server 14.04). I installed jdk7 via apt-get and Oracles Java 8 manually by extracting the tarball.

How can I switch between the Java versions from a bash session?

I suppose it should be done via "alternatives", but the details are not clear to me.

Switching java is more than calling one of the two java executables. There are other binaries (e.g. javac) and some tools refer to different files within the java installation directories (think of cacerts for example).

An optimal solution would simulate the effects of having only one of the two versions installed at any time.

Example: Using maven it is possible to set JAVA_HOME, but if some process started by maven calls java, JAVA_HOME is ignored.

I think Debian has Java 8 meanwhile. Does anybody know how they deal with this issue?

Is the alternatives mechanism only usable for individual binaries or can it be used for a complete "suite", too?

Gustave
  • 3,359
  • 4
  • 31
  • 64
  • The two Java installations are in different locations, aren't they? Especially the one you extracted and manually installed should definitely be separated from the rest of the system (like e.g. `/opt/`), and so should be easy to call the Java compiler/runtime you need. – Some programmer dude Sep 10 '15 at 09:38
  • I would have referred you to an ubuntu/linux forum, said that alternatives is fine; but then came the answer. – Joop Eggen Sep 10 '15 at 09:49
  • If you want to use the "alternatives" mechanism, have a look at this: https://wiki.debian.org/JavaPackage - the mentioned package `java-package` is also available for ubuntu (in multiverse) – mata Sep 10 '15 at 09:58
  • @mata: Thanks! Looks like this is what I am looking for. I'll try if it works that way. – Gustave Sep 11 '15 at 11:46
  • I found the following (german) article about the installation of Oracle Java 8 on Ubuntu. It includes a section about alternatives, so maybe the alternatives mechanism would work if I install it that way? – Gustave Sep 11 '15 at 11:54
  • A far as I know, OpenJDK 8 has still not been packaged for Ubuntu 14.04, true? – Gustave Sep 11 '15 at 11:58
  • The article I mentioned: https://wiki.ubuntuusers.de/Java/Installation/Oracle_Java/Java_8 – Gustave Sep 11 '15 at 11:59
  • BTW, for the maven case, using toolchains is probably the recommended way to make your builds portable, see https://maven.apache.org/plugins/maven-toolchains-plugin/usage.html – Stephan Herrmann Sep 12 '15 at 20:31
  • Possible duplicate of [How to use the command update-alternatives --config java](http://stackoverflow.com/questions/12787757/how-to-use-the-command-update-alternatives-config-java) – jopasserat Jan 26 '16 at 14:36
  • check this out: http://stackoverflow.com/a/32954607/470341 – jopasserat Jan 26 '16 at 14:36

2 Answers2

4

You can use this command to get a list of installed jdk's and easily choose one you would like to use:

sudo update-alternatives --config javac
Marcin Czyz
  • 279
  • 2
  • 5
  • This doesn't work as it is because I installed Java 8 manually: "There is only one alternative in link group javac (providing /usr/bin/javac): /usr/lib/jvm/java-7-openjdk-amd64/bin/javac Nothing to configure." Anyway, javac is not sufficient, there are more binaries. – Gustave Sep 11 '15 at 11:29
0

I'm not sure that I fully understand the question, but you could either use an environment variable in your bash session that holds the path to your java executable or you could put a symbolic link somewhere for the same purpose.

For example

export JAVA_EXEC=/usr/lib/jvm/java-8-oracle/jre/bin/java
$JAVA_EXEC -version
$JAVA_EXEC -jar cooljar.jar

Or with symlink, like the "alternatives" you mentioned

ln -s /usr/lib/jvm/java-8-oracle/jre/bin/java /usr/local/bin/java
/usr/local/bin/java -version
ln -s "${SOME_JAVA_PATH}" /usr/local/bin/java
/usr/local/bin/java -version
tonyg
  • 197
  • 2
  • 13
  • Again, java is not sufficient. Probably I should work with symlinks on whole directories. Which symlinks should I set so that the alternatives mechanism works (if that is possible and recommendable). The Java 7 installation is distributed over several directories and places, so setting up the correct symlinks appears to be non-trivial to me. – Gustave Sep 11 '15 at 11:34