18

I am writing a RHEL kickstart script, and in my %post, I need to install a JRE.

Basically, the current setup involves me needing to manually go in after first boot and set the newly installed JRE as the default using the alternatives --config command. Is there a way for me to pass arguments to alternatives so I don't have to manually pick the correct JRE?

HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
snk
  • 372
  • 3
  • 4
  • 10

4 Answers4

22

Does your version have --set?

--set name path
Set the program path as alternative for name. This is equivalent to --config but is non-interactive and thus scriptable.

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
2

You can use alternatives --auto <name> to automatically select the highest priority option.

An example:

 alternatives --install  /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/javac javac /usr/java/latest/bin/javac 10
 alternatives --install /usr/bin/javac javac /usr/java/latest/bin/javac 20
 alternatives --auto javac

Would select the higher priority version (20) /usr/java/latest/bin/javac

Daniel
  • 121
  • 4
1

Use grep:

update-alternatives --set java $(update-alternatives --list java | grep java-8)

or

update-alternatives --set java $(update-alternatives --list java | grep java-11)
Oleksii K.
  • 111
  • 3
0

You may use the script below.
You should configure the path for java "/bin" directory and a number for each version according to your system.

#!/bin/bash
# update-java script

# Supported versions from the script. You may add more version numbers according to your needs.
supported_versions=( 8 11 )

# Validate that an argument has been given
if [ -z "$1" ]; then
    echo -e "No argument given. You should set as first argument the preferred java version.\nAvailable values: ${supported_versions[@]}"
    exit
fi

# Java "/bin" of each version. Be careful, each path should have the same index with the supported_versions array.
version_path=( "/usr/lib/jvm/jdk1.8.0_171/bin" "/usr/lib/jvm/java-11-openjdk-amd64/bin")

# Configure the alternatives
found=0
for i in "${!supported_versions[@]}"; do
    if [[ ${supported_versions[$i]} -eq $1 ]]; then
        update-alternatives --set java ${version_path[$i]}/java
        update-alternatives --set javac ${version_path[$i]}/javac
        found=1
    fi
done

# If a wrong version number has been given
if [ $found -ne 1 ]; then
    echo "You didn't provide a version of: ${supported_versions[@]}"
fi

If you save the script with name "update-java", then set the executable permission, you will be able to run like below.

$sudo update-java 12
You didn't provide a version of: 8 11
$sudo update-java
No argument given. You should set as first argument the preferred java version.
Available values: 8 11
$sudo update-java 11