1

I use openjdk in my work, but sometimes I need oracle's jre or even specific version of oracle's jre. As I know, I should update JAVA_HOME value and /usr/bin/java or /etc/alternatives/java link destination. Is there a smart/secure/easy way to make these changes temporary for specific process? Thank you! (I am using debian linux unstable repo)

1

Is there a smart/secure/easy way to make these changes temporary for specific process?

Environment variables such as $JAVA_HOME are inherited, not global to the system. So if you set one a specific way, it applies only to that process, and if exported, any process it spawns.

The process here would be a shell instance; you can either do this on the command line or create something like this:

#!/bin/sh

PATH=/opt/oracle/java/bin:$PATH
export $PATH
exec whatever.jar

In case you are unfamiliar with how $PATH works, the directories are checked in order, so in this case /opt/oracle/java/bin will be searched before anything else, and when java is needed, the one there will be used.

You could elaborate this to use arguments when you invoke it to specify which .jar or .class file to execute, and some shorthand arg for which java installation to use, e.g. if [[ $1 == 2 ]]; then PATH=/opt/oracle2/java/bin....

That $PATH (and $JAVA_HOME, if you do that too) will only apply to the script and anything it starts.

If you're working on something, you can export the variables in one terminal and it will continue to apply there and only there. If you have the JDK installed, the same bin directory contains javac, etc.

  • Brilliant! '...the directories are checked in order...' that what i've totally forgotten. Was thinking how to exclude /usr/bin/java from PATH when whole dir /usr/bin is there, now it's clear. Thank you! – drozdik Jun 25 '15 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.