Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use different java version by alias?

Tags:

java

bash

shell

I have some tools that can only be run by java 8. So i downloaded java 8, but now i have two versions of java installed at the same time (os : Ubuntu):

java-1.11.0-openjdk-amd64      1101       /usr/lib/jvm/java-1.11.0-openjdk-amd64
java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64

My question is: is it possible to call these both versions by different aliases? For example i type:

  • "java" to call java 11 (the default) and
  • "java8" to call java 8
like image 316
litaoshen Avatar asked Sep 23 '18 15:09

litaoshen


People also ask

Can you have 2 different Java versions?

Yes, you can run multiple JVM's on a single machine.

Can I have different versions of Java?

If your application runs in a web browser using Java's browser plugin, you can only have one version at a time. Ignition does not use Java's browser plugin so you can continue using an older version. It is easy to specify which Java version you want to use when running Ignition's client.


1 Answers

You could do it in a couple of ways. The easiest way would be to put the following 2 lines in your profile initialization file:

alias java='/usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java'
alias java8='/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin/java'

Other ways are depending on whether you are the admin of the machine or not. You can create soft links like this: /usr/bin/java -> /usr/lib/jvm/java-1.11.0-openjdk-amd64 (Not recommended since certain tools in your system may depend on /usr/bin/java).

Update: Try to use sdkman if you can. Makes the job of installing java versions and setting up JAVA_HOME env variable simple.

like image 93
apatniv Avatar answered Sep 30 '22 13:09

apatniv