Setting JDK 7 as default

 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?


Solution 1:

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.

Solution 2:

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

Solution 3:

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

Solution 4:

the GUI way

$ sudo apt-get install galternatives

$ sudo galternatives

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