Home > Software design >  running a java program under a specific version of java
running a java program under a specific version of java

Time:04-16

java versions installed on my linux machine

sudo update-alternatives --config java

/usr/lib/jvm/java-17-oracle/bin/java

/usr/lib/jvm/java-17-oracle/bin/java

/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

how do i make a file .sh, which runs my program under java version 1.8 without changing the standard version of java

My script does not work

/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/ --exec java /home/alex/xmind/XMind_amd64/XMind

Thanks

CodePudding user response:

To do that, you need to understand how a program is called from the shell.

If your technology is called acme, located /usr/foo/acme-app/bin/acme, you should add it to the special variable environment called PATH

export ACME_HOME=/usr/foo/acme-app/bin
export PATH=$PATH:$ACME_HOME/bin

Just after that, acme could be used in the shell

acme --help

Some installers do it at installation step but in some cases you need to do it manually.

In your case to have a specific version

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
export PATH=$PATH:$JAVA_HOME/bin

or

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin

After that you could confirm the version with

java -version

Sometimes the PATH is dirty with due to your previous installations. I mean in the PATH there are several java versions. You could check with

echo $PATH

If there are several java versions in the path, you just need to erase them and export a new PATH. Here an example of clean and minimal PATH:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

And other no so clean

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-17-oracle/bin:/usr/lib/jvm/java-8-openjdk-amd64:/usr/lib/jvm/java5

In that case, you just need to fix the PATH and add only one java version

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
export PATH=$PATH:$JAVA_HOME/bin

Note: This works while the shell is open. If it is closed or in another shell, PATH is restored. Toset this permanently, check this: How to permanently set $PATH on Linux/Unix

CodePudding user response:

The shortest way. Declare JAVA_HOME Call java from JAVA_HOME

JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
$JAVA_HOME/jre/bin/java /path/to/your/program
  • Related