2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> update-java-alternatives.sh"
12 # This script takes the java-version variable to set the proper alternative
13 # for java, javac and java_sdk_openjdk for ubuntu or centos/fedora/redhat distros
15 JAVA_ENV_FILE="/tmp/java.env"
17 JAVA_RELEASE=$(echo "$SET_JDK_VERSION" | sed 's/[a-zA-Z]//g')
18 JAVA_RELEASE_NBR=$(echo "$SET_JDK_VERSION" | sed 's/[a-zA-Z:-]//g')
19 #TODO check whether is it worth keeping there 2 distinct variables
20 update_java_redhat() {
21 if [ "${JAVA_RELEASE}" -ge 9 ]; then
22 # Java 9 or newer: new version format
23 export JAVA_HOME="/usr/lib/jvm/java-${JAVA_RELEASE}-openjdk"
25 # Java 8 or older: old version format
26 export JAVA_HOME="/usr/lib/jvm/java-1.${JAVA_RELEASE_NBR}.0-openjdk"
30 update_java_ubuntu() {
31 HOST_ARCH=$(dpkg --print-architecture)
32 export JAVA_HOME="/usr/lib/jvm/java-${JAVA_RELEASE_NBR}-openjdk-${HOST_ARCH}"
35 echo "---> Updating Java version"
36 OS=$(facter operatingsystem | tr '[:upper:]' '[:lower:]')
40 echo "---> RedHat type system detected"
42 alternatives="/usr/sbin/alternatives"
45 echo "---> Ubuntu/Debian system detected"
47 alternatives=$(which update-alternatives)
51 if ! [ -d "$JAVA_HOME" ]; then
52 echo "$JAVA_HOME directory not found - trying to find an approaching one"
53 if ls -d "$JAVA_HOME"*; then
54 # shellcheck disable=SC2012
55 JAVA_HOME=$(ls -d "$JAVA_HOME"* | head -1)
58 echo "no $JAVA_HOME directory nor candidate found -exiting " >&2
63 # If sudo is not found, the commands below will run anyway
64 SUDO_CMD=$(which sudo)
66 $SUDO_CMD "$alternatives" --install /usr/bin/java java "${JAVA_HOME}/bin/java" 1
67 $SUDO_CMD "$alternatives" --install /usr/bin/javac javac "${JAVA_HOME}/bin/javac" 1
68 $SUDO_CMD "$alternatives" --install /usr/lib/jvm/java-openjdk java_sdk_openjdk "${JAVA_HOME}" 1
69 $SUDO_CMD "$alternatives" --set java "${JAVA_HOME}/bin/java"
70 $SUDO_CMD "$alternatives" --set javac "${JAVA_HOME}/bin/javac"
71 $SUDO_CMD "$alternatives" --set java_sdk_openjdk "${JAVA_HOME}"
72 echo JAVA_HOME="$JAVA_HOME" > "$JAVA_ENV_FILE"
75 echo JAVA_HOME="${JAVA_HOME}"