Configuring and switching Java versions on Linux using alternatives

suggest change

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
  1. 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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Installing Java Standard Edition:
* Configuring and switching Java versions on Linux using alternatives

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
97 Installing Java Standard Edition
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap