-2

I have two versions of java running on my local server. I wish to use one java version for 1 application and another java version for another application. The user is same. I have been trying to explore different options and I am stuck now. Can somebody please guide me how to go about it

alternatives --config java

There are 2 programs which provide 'java'.

Selection Command

*+ 1 /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java 2 /usr/java/jdk1.8.0_101/bin/java

The default version is 1.7 but I wish to run a script which displays the java version as 1.8. Basically, I wish to use java 1.8 only for that bash script without changing the default version of java running on the machine.

I have created a bash script with the export path of the java 1.8

export JAVA_HOME=/usr/java/jdk1.8.0_101/bin/java export PATH=$PATH:/usr/java/jdk1.8.0_101/bin JAVA_VERSION=java -version 2>&1 |awk 'NR==1{ gsub(/"/,""); print $3 }'

export JAVA_VERSION

echo $JAVA_VERSION

The output of this bash script still displays the java version as 1.7. Am I doing something wrong?

3
  • 2
    java is regular program. You can run it with full path. What you want to achieve?
    – talex
    Sep 30, 2016 at 12:32
  • When you want to run a particular version of java you give it the full path name. You can only have one default version of Java in your PATH (though you can set the PATH differently for each program) I have 8 version of java on one machine. Sep 30, 2016 at 12:34
  • I have created a bash script with the export path of the java 1.8 export JAVA_HOME=/usr/java/jdk1.8.0_101/bin/java export PATH=$PATH:/usr/java/jdk1.8.0_101/bin JAVA_VERSION=java -version 2>&1 |awk 'NR==1{ gsub(/"/,""); print $3 }' export JAVA_VERSION echo $JAVA_VERSION The output of this bash script still displays the java version as 1.7. Am I doing something wrong? Sep 30, 2016 at 13:03

1 Answer 1

1

You reference the full path when kicking off the JVM. I don't really know the context, but this could be via a cron job or shell script or configured in your app server.

For example:

/usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java Main

/usr/java/jdk1.8.0_101/bin/java Main2
2
  • Thanks Andrew. Will use the fix provided by you and how it goes. Sep 30, 2016 at 12:44
  • You should also probably set the JAVA_HOME env variable.
    – slim
    Sep 30, 2016 at 12:57

Your Answer

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

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