I want to be able to ‘script’ the setting of java 11 as the system java, but have not been able to do so reliably, across multiple hosts. Issuing the command “alternatives --config java” displays java-11 and java-1.8.0, not always in the same order, so I cannot reliably set java-11 as the system default version since it might be first in the list or second in the list, depending on the Rocky Linux hosts I’m on. The command that I’m scripting
sudo echo 1 | sudo alternatives --config java
This works great, if java 11 is first in the list of alternatives
If java 1.8 is first, obviously it sets that as system default java. What is the best way to reliably set java 11 as system default?
Ask yourself, what does the ‘alternatives’ do? Does it simply update symlinks?
If that is all it does, can you create the same symlinks?
There is an Ansible module for updating alternatives: https://docs.ansible.com/ansible/latest/collections/community/general/alternatives_module.html
Examples in that module doc seem to be for … Java. The Ansible is basically “a script”.
You can script it, first use a command to get the item number:
echo | alternatives --config java | grep java-11 | awk '{print $1}'
in your script you can save that to a variable easy enough, so:
JAVA_VER=$(echo | alternatives --config java | grep java-11 | awk '{print $1}')
echo ${JAVA_VER} | alternatives --config java > /dev/null
There’s probably a way to do this by solely using awk without grep, but my awk foo is not that brilliant to be able to do that.
And a test on my system:
root@rocky9:~# alternatives --config java
There are 3 programs which provide 'java'.
Selection Command
-----------------------------------------------
*+ 1 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.15.0.6-2.el9.x86_64/bin/java)
2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.452.b09-2.el9.x86_64/jre/bin/java)
3 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.25.0.9-7.el9.x86_64/bin/java)
Enter to keep the current selection[+], or type selection number: ^C
root@rocky9:~# cat java.sh
#!/bin/bash
JAVA_VER=$(echo | alternatives --config java | grep java-11 | awk '{print $1}')
echo ${JAVA_VER} | alternatives --config java > /dev/null
root@rocky9:~# ./java.sh
root@rocky9:~# alternatives --config java
There are 3 programs which provide 'java'.
Selection Command
-----------------------------------------------
* 1 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.15.0.6-2.el9.x86_64/bin/java)
2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.452.b09-2.el9.x86_64/jre/bin/java)
+ 3 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.25.0.9-7.el9.x86_64/bin/java)
Enter to keep the current selection[+], or type selection number: ^C
The above assumes only packages installed via dnf. If someone has manually unpacked and configured Java 11 say under /usr/local and added it to alternatives, then it will require some additional work to filter that out in the initial command to get the number from alternatives.
Hmm, ok I see but the * and + get in the way for me. In my env, if java 1.8 is the selected java, the * is set for java-11 and the $1 token value is *, but I get your main point and can try to get that to work, consistently. Someone asked, why not use symbolics. I decided not to create my own symbolics, trying to use a functionality that is supposed to deal with this.
I also happened upon an alternative syntax that I got to work, that seems promising
Running the command
sudo update-alternatives --config java
There are 2 programs which provide ‘java’.
Selection Command
- 1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.25.0.9-3.el9.x86_64/bin/java)
- 2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.452.b09-2.el9.x86_64/jre/bin/java)
So the following would set java-11 as system java
sudo update-alternatives --set java java-11-openjdk.x86_64
sudo update-alternatives --config java
There are 2 programs which provide ‘java’.
Selection Command
*+ 1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.25.0.9-3.el9.x86_64/bin/java)
2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.452.b09-2.el9.x86_64/jre/bin/java)
I’m leaning towards this as a solution, assuming its standard syntax
1 Like