160

On Ubuntu 12.04 LTS I have installed Sun's JDK7, Eclipse, and the Arduino IDE. I want the Arduino to use OpenJDK 6 and want Eclipse to use Sun's JDK 7.

From my understanding I need to manually choose which Java to use before running each application. This led me to the update-java-alternatives -l command. When I run this I only see the following:

java-1.6.0-openjdk-amd64 1061 /usr/lib/jvm/java-1.6.0-openjdk-amd64

but when I run update-alternatives --config java I see the following:

*0  /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   auto mode
1  /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   manual mode
2  /usr/lib/jvm/jdk1.7.0/bin/java                   manual mode
3  /usr/lib/jvm/jre1.7.0/bin/java                   manual mode

I don't understand why the update-java-alternatives doesn't display the same 3 options. I also don't understand how to switch between OpenJDK6 and JDK7. How I can go about using the OpenJDK6 for Arduino development and Sun JDK7 for Eclipse/Android development?

4 Answers 4

199
sudo update-alternatives --config java

Configures the default for the program "java". That's the Java VM.

sudo update-alternatives --config javac

Configures the default Java compiler.

You can also see that, because the first command lists a lot of "JRE" (Java Runtime Environment) folders and the Program is just called "java".

If I check which version is being used by issuing the command java -version or javac -version, I can see, that each command changes the program being used.

However, using update-java-alternatives with a JDK Version changes both programs for me. Using the first commands, you can use a Java VM and Java Compiler from different JDKs.

update-java-alternatives requires presence of a file with extension .jinfo in directory /usr/lib/jvm. The openjdk package is shipped with a .jinfo file, the jdk of Oracle (formerly Sun) is not. As alternative, you configure alternatives without update-java-alternatives:

For example, to add java from jvm-directory /usr/lib/jvm/jdk-12.0.1 (default directory of Debian package of Oracle) with priority 2082, use the following command:

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-12.0.1/bin/java 2082

As for switching for different development environments:

Are you talking about starting the IDE itself with different Java versions or using different versions in the IDE for compilation and running your app?

  • For 1.: You can specify which JVM to use in the eclipse.ini, as described here. I don't know how to do that for the Arduino IDE.

  • For 2.: In Eclipse you can select the JRE/JDK to be used in Window -> Preferences -> Java -> Installed JREs. And under Java -> Compiler you could choose an older Java compliance if you wish.

EDIT: This DigitalOcean page also has a very nice explanation of everything related to Java on Ubuntu.

35

update-java-alternatives is a program to update alternatives for jre/jdk installations.

update-alternatives is a symbolic link management system for linux (I'm sure there is little news here).

You can, and really should, use both update-java-alternatives and update-alternatives together.

Firstly, be sure to have the all the alternatives configured correctly. java and javac are but a few. There is javadoc, rmic, serialver and others, substituting the above variables for: native2ascii and /opt/jdk1.8.0_40/bin/native2ascii should report if the alternative is installed and/or selected.

When all the alternatives are configured you can then create links in /usr/lib/jvm to your manual instalation.

In order to configure update-java-alternatives you must use a hidden file with the same name as your directory but prefixed by a . (dot).

Hope this helps.

Bibliography

man -S 8 update-java-alternatives

http://tech.lanesnotes.com/2008/03/using-alternatives-in-linux-to-use.html

https://stackoverflow.com/questions/6477415/how-to-set-oracles-java-as-the-default-java-in-ubuntu

1
  • 3
    when i run update-java-alternatives it does update the symbolic links in /etc/alternatives. So you don't need to run both.
    – dan carter
    Mar 1, 2020 at 23:25
5

Just a side note (too big for a comment). If you need to automatically switch to Java 8 (compiler 1.8), in an one-liner, for example for a script or continuous integration test suite, you can run

sudo update-java-alternatives -s $(sudo update-java-alternatives -l | grep '1\.8' | cut -d " " -f1) || echo '.'

It will automatically fetch any java 8 version available and set it using the command update-java-alternatives.

The || echo '.' at the end just ensures the command returns success, because strangely update-java-alternatives by default returns error (1). You may skip it if you don't need this for a test suite.

1

I've checked the content of /usr/sbin/update-java-alternatives and it's just a script that calls update-alternatives for every entry in the file /usr/lib/jvm/.<jname>.jinfo.

If you have no such file for your new JDK installation, you can create your own installation script as follows. Let's suppose you've just installed JDK 17 on Ubuntu via a Debian package and you have this existing situation:

$ update-alternatives --config java

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-8-openjdk-amd64/jre/bin/java   1081      manual mode
* 2            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      manual mode
  3            /usr/lib/jvm/jdk-13.0.2/bin/java                 1081      manual mode
^CTRL-C

Create the following script:

$ sudo nano jdk-17.0.6.install-alternatives.sh 

with the following content, making sure you substitute the right values for the JAVADIR, PRIORITY and BINDIR variables according to your system's configuration:

#!/bin/bash

JAVADIR=/usr/lib/jvm/jdk-17
PRIORITY=1081
BINDIR=/usr/bin
find "$JAVADIR" -perm 755 -type f | while read FP
do
  FN=$(basename $FP)
  echo "update-alternatives --install /usr/bin/$FN $FN $FP $PRIORITY"
  update-alternatives --install /usr/bin/$FN $FN $FP $PRIORITY
done

This will pick all the executable files in your new Java package, even those under directories different from bin/ (i.e. lib/).

Make the script executable:

$ chmod 755 jdk-17.0.6.install-alternatives.sh

then run it as root:

$ sudo ./jdk-17.0.6.install-alternatives.sh 
update-alternatives --install /usr/bin/jspawnhelper jspawnhelper /usr/lib/jvm/jdk-17/lib/jspawnhelper 1081
update-alternatives --install /usr/bin/jexec jexec /usr/lib/jvm/jdk-17/lib/jexec 1081
update-alternatives --install /usr/bin/keytool keytool /usr/lib/jvm/jdk-17/bin/keytool 1081
update-alternatives --install /usr/bin/jmod jmod /usr/lib/jvm/jdk-17/bin/jmod 1081
update-alternatives --install /usr/bin/jimage jimage /usr/lib/jvm/jdk-17/bin/jimage 1081
update-alternatives --install /usr/bin/jstat jstat /usr/lib/jvm/jdk-17/bin/jstat 1081
update-alternatives --install /usr/bin/jdb jdb /usr/lib/jvm/jdk-17/bin/jdb 1081
update-alternatives --install /usr/bin/jstack jstack /usr/lib/jvm/jdk-17/bin/jstack 1081
update-alternatives --install /usr/bin/jcmd jcmd /usr/lib/jvm/jdk-17/bin/jcmd 1081
update-alternatives --install /usr/bin/serialver serialver /usr/lib/jvm/jdk-17/bin/serialver 1081
update-alternatives --install /usr/bin/jinfo jinfo /usr/lib/jvm/jdk-17/bin/jinfo 1081
update-alternatives --install /usr/bin/jps jps /usr/lib/jvm/jdk-17/bin/jps 1081
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 1081
update-alternatives --install /usr/bin/jhsdb jhsdb /usr/lib/jvm/jdk-17/bin/jhsdb 1081
update-alternatives --install /usr/bin/jlink jlink /usr/lib/jvm/jdk-17/bin/jlink 1081
update-alternatives --install /usr/bin/jstatd jstatd /usr/lib/jvm/jdk-17/bin/jstatd 1081
update-alternatives --install /usr/bin/jdeprscan jdeprscan /usr/lib/jvm/jdk-17/bin/jdeprscan 1081
update-alternatives --install /usr/bin/jshell jshell /usr/lib/jvm/jdk-17/bin/jshell 1081
update-alternatives --install /usr/bin/jdeps jdeps /usr/lib/jvm/jdk-17/bin/jdeps 1081
update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk-17/bin/jar 1081
update-alternatives --install /usr/bin/jfr jfr /usr/lib/jvm/jdk-17/bin/jfr 1081
update-alternatives --install /usr/bin/jarsigner jarsigner /usr/lib/jvm/jdk-17/bin/jarsigner 1081
update-alternatives --install /usr/bin/javadoc javadoc /usr/lib/jvm/jdk-17/bin/javadoc 1081
update-alternatives --install /usr/bin/jpackage jpackage /usr/lib/jvm/jdk-17/bin/jpackage 1081
update-alternatives: using /usr/lib/jvm/jdk-17/bin/jpackage to provide /usr/bin/jpackage (jpackage) in auto mode
update-alternatives --install /usr/bin/jmap jmap /usr/lib/jvm/jdk-17/bin/jmap 1081
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-17/bin/javac 1081
update-alternatives --install /usr/bin/jconsole jconsole /usr/lib/jvm/jdk-17/bin/jconsole 1081
update-alternatives --install /usr/bin/javap javap /usr/lib/jvm/jdk-17/bin/javap 1081
update-alternatives --install /usr/bin/rmiregistry rmiregistry /usr/lib/jvm/jdk-17/bin/rmiregistry 1081
update-alternatives --install /usr/bin/jrunscript jrunscript /usr/lib/jvm/jdk-17/bin/jrunscript 1081

Now you have the following updated situation for all the executables listed above:

$ update-alternatives --config java
There are 4 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-8-openjdk-amd64/jre/bin/java   1081      manual mode
* 2            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      manual mode
  3            /usr/lib/jvm/jdk-13.0.2/bin/java                 1081      manual mode
  4            /usr/lib/jvm/jdk-17/bin/java                     1081      manual mode

Press <enter> to keep the current choice[*], or type selection number:

^CTRL-C

$ update-alternatives --config jshell
There are 2 choices for the alternative jshell (providing /usr/bin/jshell).

  Selection    Path                                Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/jdk-13.0.2/bin/jshell   1081      auto mode
  1            /usr/lib/jvm/jdk-13.0.2/bin/jshell   1081      manual mode
  2            /usr/lib/jvm/jdk-17/bin/jshell       1081      manual mode

Press <enter> to keep the current choice[*], or type selection number: ^C

$ update-alternatives --config jpackage
There is only one alternative in link group jpackage (providing /usr/bin/jpackage): /usr/lib/jvm/jdk-17/bin/jpackage
Nothing to configure.

^CTRL-C

And so on.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .