85
 four@twenty:~$ file /etc/alternatives/java /etc/alternatives/javac
 /etc/alternatives/java:symbolic link to `/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java'
 /etc/alternatives/javac:symbolic link to `/usr/lib/jvm/java-6-openjdk-amd64/bin/javac'

Forgive my noobishness but I am interpreting these messages as the runtime environment using Java 7 but the compiler is using Java 6?

Is this right, and if so, how can I set it to use Java version 7?

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
Wingsuit
  • 969

4 Answers4

159

Switching between installed Java versions can be accomplished using the update alternatives command.

  • To get a list of your installed Java platforms, run the following command from the terminal:

    sudo update-alternatives --config java
    

    This will give you a list output similar to this:

    There are 2 choices for the alternative java (providing /usr/bin/java).
       Selection    Path                                           Priority   Status
      ------------------------------------------------------------
      0            /usr/lib/jvm/java-6-oracle/jre/bin/java         1070      auto mode
      1            /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java   1051      manual mode
    * 2            /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java   1069      manual mode
    Press enter to keep the current choice[*], or type selection number: 
    

In this case, the Open JDK 6 version is running. To switch to the Open JDK version 7, you would select option 1 at the prompt and press the Enter key.

You will now be running the OpenJDK 7 version. No other changes will be needed to switch your Java versions.

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
  • Okay thanks. Would I gain anything from uninstalling jkd6? – Wingsuit Mar 26 '13 at 05:40
  • @user142712 No problem. I believe that an uninstall would also remove the older browser plugin, which is beneficial security-wise. – Kevin Bowen Mar 26 '13 at 05:52
  • 1
    I wouldn't go for the "No problem" answer so fast. Check http://www.oracle.com/technetwork/java/javase/compatibility-417013.html and http://upstream-tracker.org/java/versions/jdk.html. There is no 100% compatibility between those two major versions and it might happen so (unlikely but still...) that you have an application X that cannot run properly with the seventh version. Until you make sure there will be no issues I strongly advice to keep both just in case you need the previous version. – rbaleksandar Jul 03 '14 at 15:02
  • Just to be clear. The "no problem" comment was another way of me saying you are welcome, I never claimed that there would be no problems with your Java installation. – Kevin Bowen Apr 30 '15 at 00:55
  • you need to do this for javac as well in case you wanna change the compiler as well. – samsamara Feb 23 '16 at 05:01
  • 1
    update-alternatives solution provided by this answer will switch JRE only. See below for the proper way to switch entire JDK (compiler and other tools) with one command – J. Will Mar 25 '17 at 10:19
17

The most voted answer which suggests to use

update-alternatives

only partially answers the question. By using it you very likely to end up with your Java environment being inconsistent.

This is especially important if you have JDK, not JRE, as

[sudo] update-alternatives --config java

will change version for the the java command only, i.e. repoint main JRE executable.

javac and other JDK tools will still point to the other java version. To fully switch you have to continue with update-alternatives for all other tools.

The proper and much easier way to switch Java versions in the Linux distribution with "alternatives" system - i.e. in the Debian or it's derivative Ubuntu - is to use update-java-alternatives command.

update-java-alternatives -l - will list installed Java versions

[sudo] update-java-alternatives -s - will switch to the requested version

Source

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
J. Will
  • 312
10

You can run sudo update-alternatives --config java, and select JDK 7, if it's installed.

  • Okay cheers, It says I have two of JKD7 installed one listing status as auto the other manual. Is there any difference? – Wingsuit Mar 26 '13 at 05:44
  • There's Info here: http://linux.die.net/man/8/update-alternatives but i'd select auto. – Frederik Spang Mar 26 '13 at 07:45
  • "Each link group is, at any given time, in one of two modes: automatic or manual. When a group is in automatic mode, the alternatives system will automatically decide, as packages are installed and removed, whether and how to update the links. In manual mode, the alternatives system will not change the links; it will leave all the decisions to the system administrator. " – mpen Dec 17 '13 at 19:12
6

the GUI way

$ sudo apt-get install galternatives

$ sudo galternatives

more here: https://askubuntu.com/a/159594/216936