update-java-alternatives vs update-alternatives --config java
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-amd64but 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 modeI 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?
3 Answers
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 commandjava -versionorjavac -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 opendjk 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.
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 1 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 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.