Java Language Installing Java (Standard Edition) Configuring and switching Java versions on Linux using alternatives


Example

Using Alternatives

Many Linux distributions use the alternatives command for switching between different versions of a command. You can use this for switching between different versions of Java installed on a machine.

  1. In a command shell, set $JDK to the pathname of a newly installed JDK; e.g.

    $ JDK=/Data/jdk1.8.0_67
    
  2. Use alternatives --install to add the commands in the Java SDK to alternatives:

     $ sudo alternatives --install /usr/bin/java java $JDK/bin/java 2
     $ sudo alternatives --install /usr/bin/javac javac $JDK/bin/javac 2
     $ sudo alternatives --install /usr/bin/jar jar $JDK/bin/jar 2
    

    And so on.

Now you can switch between different versions of a Java command as follows:

$ sudo alternatives --config javac

There is 1 program that provides 'javac'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.101-1.b14.fc23.x86_64/bin/javac
   2           /Data/jdk1.8.0_67/bin/javac

Enter to keep the current selection[+], or type selection number: 2
$

For more information on using alternatives, refer to the alternatives(8) manual entry.

Arch based installs

Arch Linux based installs come with the command archlinux-java to switch java versions.

Listing installed environments

$ archlinux-java status
Available Java environments:
  java-7-openjdk (default)
  java-8-openjdk/jre

Switching current environment

# archlinux-java set <JAVA_ENV_NAME>

Eg:

# archlinux-java set java-8-openjdk/jre

More information can be found on the Arch Linux Wiki