I have java version 6,7,8 installed on my Ubuntu server, the result of update-alternatives --config java is

There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                     Priority   Status
  0            /usr/lib/jvm/java-8-oracle/jre/bin/java   1081      auto mode
  1            /usr/lib/jvm/java-6-oracle/jre/bin/java   2         manual mode
* 2            /usr/lib/jvm/java-7-oracle/jre/bin/java   1         manual mode
  3            /usr/lib/jvm/java-8-oracle/jre/bin/java   1081      manual mode

I tried How to set the default Java version to set my current java version. when I do java -version I am getting:

java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

which is correct but at runtime I getting the version as

Java Home: /usr/lib/jvm/java-6-oracle/jre

which is the result of

System.out.println("Java Home: "+System.getProperty("java.home"))

How do I set java7?

  • 1
    How do you start the Java program that prints the wrong home? Assuming you start it in a terminal somehow, what is $JAVA_HOME in that terminal? – Harald Mar 25 '17 at 17:29
  • Java program is a web application and the statement gets printed after deploying the app in the container and making the request to the resource which contains the print statment. – Abdullah Qayum Mar 25 '17 at 17:36
  • and $JAVA_HOME is set to /usr/lib/jvm/java-7-oracle – Abdullah Qayum Mar 25 '17 at 17:37

Open /etc/environment and manually set the JAVA_HOME Directory.

sudo nano /etc/environment

In your case to set Java 7 add the following line in /etc/environment


Then update the source by executing the following command:

source /etc/environment
  • I am using bashrc to set the Java home and after setting the Java home here I used source command to reload the changes but no luck – Abdullah Qayum Mar 26 '17 at 8:31
  • Are you trying to run Java from terminal or any third party applications like eclipse?? – shahin Mar 26 '17 at 8:37
  • this askubuntu.com/a/154967/201077 resolved my issue – Abdullah Qayum Mar 27 '17 at 6:36

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.