Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify which version of Java to use at terminal? [duplicate]

A CentOS 7 devbox has Java 8 installed. I need to use Java 7 to compile jars. So I downloaded and installed Java 7, but java -version still shows Java 8. How can I make sure that Java 7 is used to compile a jar at the command line?

Here are the steps I took to install Java 7:

# wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/7u75-b13/jdk-7u75-linux-x64.rpm  
# rpm -ivh jdk-7u75-linux-x64.rpm
# export JAVA_HOME=/usr/java/latest
# java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

As you can see, java -version still points to Java 8, even though /usr/java/latest points to the Java 7 version that was just downloaded and installed.

The command I want to run is java -jar gs-actuator-service-0.1.0.jar. Can someone show how to change the syntax in that specific command to specify the Java 7 version? Or do I need to remove Java 8? If so, what specific steps do I take?

like image 467
CodeMed Avatar asked Dec 28 '15 20:12

CodeMed


2 Answers

If your goal is to change to that specific Java version as a one-time thing for the current session, then you can add the following to your list of commands.

export PATH=$JAVA_HOME/bin:$PATH

If your goal is to do a more permanent switch to a specific version, then you can use the alternatives command to switch to Java 7 as the default.

alternatives --config java
like image 117
Chris Nauroth Avatar answered Oct 18 '22 22:10

Chris Nauroth


${JAVA_HOME}/bin/java -version

There is nothing magic about the JAVA_HOME environment variable. It is simply a common convention for use in shell scripts that launch java apps. A defined variable does nothing if it is not used.

like image 35
Steve Cohen Avatar answered Oct 19 '22 00:10

Steve Cohen