Gustave
Gustave

Reputation: 3528

What is the recommended approach to switch between java 7 and java 8 in Linux (Ubuntu)?

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?

Upvotes: 2

Views: 1949

Answers (2)

Marcin Czyz
Marcin Czyz

Reputation: 279

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

Upvotes: 4

tonyg
tonyg

Reputation: 197

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

Upvotes: 0

Related Questions