Home > Software design >  Regarding having multiple JAVA_HOME on the same machine
Regarding having multiple JAVA_HOME on the same machine

Time:07-25

I want to know whether we can have multiple JAVA_HOME on the same machine. If so, what are the steps to be followed to have multiple JAVA_HOME..? please help me with the answer...

CodePudding user response:

If you want to use a different version of Java for different projects you will need to specify it in the project. The JAVA_HOME can only be one version.

CodePudding user response:

in ubuntu, it works for me:
step 1: install muti version java.
step 2: example, I run muti java with maven.

  • edit file barchrc:

    vim ~/.bashrc

  • add new line:

    JAVA_HOME=/usr/java/openjdk/jdk-15
    export PATH=$PATH:/usr/local/nodejs/bin:$JAVA_HOME/bin
    export JAVA_HOME
    alias mvn8="JAVA_HOME=/usr/lib/jvm/jdk1.8.0_261 && mvn"
    alias mvn11="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 && mvn"
    alias mvn15="JAVA_HOME=/usr/java/openjdk/jdk-15 && mvn"

  • after, source file brach:

    source ~/.bashrc

  • run test.mvn8 clean compile or mvn11 clean compile

enter image description here

CodePudding user response:

you can use jenv, with the export plugin enabled to change the java version and JAVA_HOME like

Configure global version

$ jenv global openjdk64-11.0.8

Configure local version (per directory)

$ jenv local openjdk64-11.0.8

Configure shell instance version

$ jenv shell openjdk64-11.0.8

To make sure JAVA_HOME is set, make sure to enable the export plugin:

jenv enable-plugin export
exec $SHELL -l

further information at https://www.jenv.be/

  •  Tags:  
  • java
  • Related