1

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

1
  • What's the command to run it without specifying a version? Commented Apr 15, 2022 at 16:41

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

5 Comments

This is bad advice. Though this kind of works, most Java tools that use JAVA_HOME would expect it to point to "/usr/lib/jvm/java-1.8.0-openjdk-amd64/", not "/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin".
@StephenC: changed
Still not right
For example, later Java versions don't have a jre directory ... in the OpenJDK packaging for Ubuntu for example.
@StephenC .. I see.. Thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.