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.
Answer from Benjamin Maurer on askubuntu.comsudo 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.
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
Configure alternatives for java 11
bash - setting JAVA_HOME and PATH with update-alternatives - Unix & Linux Stack Exchange
how can I change the default Java version?
How do i change default java version on Linux Mint
Videos
Assuming one has installed a JDK in /opt/java/jdk1.8.0_144 then:
Install the alternative for javac
$ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.8.0_144/bin/javac 1Check / update the alternatives config:
$ sudo update-alternatives --config javac
If there is only a single alternative for javac you will get a message saying so, otherwise select the option for the new JDK.
To check everything is setup correctly then:
$ which javac
/usr/bin/javac
$ ls -l /usr/bin/javac
lrwxrwxrwx 1 root root 23 Sep 4 17:10 /usr/bin/javac -> /etc/alternatives/javac
$ ls -l /etc/alternatives/javac
lrwxrwxrwx 1 root root 32 Sep 4 17:10 /etc/alternatives/javac -> /opt/java/jdk1.8.0_144/bin/javac
And finally
$ javac -version
javac 1.8.0_144
Repeat for java, keytool, jar, etc as needed.
You will notice a big change when selecting options if you type in "java -version" after doing so. So if you run update-alternatives --config java and select option 3, you will be using the Sun implementation.
Also, with regards to auto vs manual mode, making a selection should take it out of auto mode per this page stating:
When using the
--configoption, alternatives will list all of the choices for the link group of which given name is the master link. You will then be prompted for which of the choices to use for the link group. Once you make a change, the link group will no longer be inauto mode. You will need to use the--autooption in order to return to the automatic state.
And I believe auto mode is set when you install the first/only JRE/JDK.
Thast's because you have altered IFS variable to use ':'
so when its output your variable its replaced with default output field separator which is 'space' thinking that ':' is input field separator .
you have take backup of it before using IFS like below :
OIFS=$IFS
IFS=':';
after 'for' loop is done , restore it :
IFS=$OIFS
Also remove the ':' which starts with no path preceding that
PATH=${PATH#:*}
Your script should be like :
#!/bin/bash
export JAVA_HOME=$(dirname $(dirname `readlink -f /etc/alternatives/java`))
OIFS=
PATH;
do
JAVA1=$i/bin/java
JAVA2=$i/java
if [ -d "$i" ];
then
if [ ! -L "$JAVA1" ] && [ -x "$JAVA1" ] || [ ! -L "$JAVA2" ] && [ -x "$JAVA2" ];
then
echo "dropping path: $i";
else
NEW=
i
fi
fi
done
IFS=$OIFS
PATH=
JAVA_HOME/bin
PATH=${PATH#:*}
echo
echo "Final:"
echo $PATH
Why not set the current java version with:
sudo update-alternatives --config java
sudo update-alternatives --config javac
(and whatever else java binary you need)
Then set the following in your .bashrc or .zshrc:
#!/bin/bash
if [ -z "${JAVA_HOME}" ]
then
JAVA_HOME=$(readlink -nf $(which java) | xargs dirname | xargs dirname)
if [ ! -e "$JAVA_HOME" ]
then
JAVA_HOME=""
fi
export JAVA_HOME=$JAVA_HOME
fi
script source
This is sufficient to have a configured java enviroment, there's no need to set the PATH variable, as it's already managed by the alternatives framework.
Cheers F.