Skip to content

Commit

Permalink
select-java: query java version using -XshowSettings
Browse files Browse the repository at this point in the history
instead of parsing the output of `-version`, parse output of
`-XshowSettings:properties`, whose format is more predicable.
as on EL9, we have

> Picked up JAVA_TOOL_OPTIONS: openjdk version "11.0.19" 2023-04-18 LTS OpenJDK Runtime Environment (Red_Hat-11.0.19.0.7-1.el9_1) (build 11.0.19+7-LTS) OpenJDK 64-Bit Server VM (Red_Hat-11.0.19.0.7-1.el9_1) (build 11.0.19+7-LTS, mixed mode, sharing)

while on fedora 38, jre-1.8 prints

> openjdk version "1.8.0_362"

jre-11 prints

> openjdk version "11.0.19" 2023-04-18

before this change, we just use `"` as the field separator for awk,
this works, but the parsing algorithm is fragile. so let's use the
"java.specification.version" property instead, it is always printed
like

>     java.specification.version = 1.8

so, after this change, we split this line with " = ", and pick the
last field, i.e., the value of `java.specification.version`.
also, in this change, we are using the case clause for better readability.

also, in this change, instead of dispatch the logic by the distro
id, we just check all JVMs located under /usr/lib/jvm, and pick the
first match. this simplifies the existing complicated implementation.

also, instead of silently returning status code of 1, print error messages
for better user experience.

Fixes scylladb#212
Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
  • Loading branch information
tchaikov committed May 16, 2023
1 parent f176bcd commit 3e62ffe
Showing 1 changed file with 18 additions and 41 deletions.
59 changes: 18 additions & 41 deletions scripts/select-java
Original file line number Diff line number Diff line change
@@ -1,52 +1,29 @@
#!/bin/bash

function select_java_debian() {
local java
local arch
arch=$(dpkg --print-architecture)
function select_java() {
local expected_java_versions
expected_java_versions=$@

for version in "11" "8"; do
java=/usr/lib/jvm/java-${version}-openjdk-${arch}/bin/java
if [ -e $java ]; then
echo $java
return
fi
for expected_java_version in $expected_java_versions; do
for java_home in /usr/lib/jvm/*; do
java=$java_home/bin/java
javaver=$($java -XshowSettings:properties -version 2>&1 | awk -F' = ' '/java.specification.version/ {print $NF}')
echo $javever
if [ "$javaver" = $expected_java_version ]; then
echo $java
return
fi
done
done
}

function select_java_fedora() {
local java
for version in "11" "1.8.0"; do
java=/usr/lib/jvm/jre-${version}-openjdk/bin/java
if [ -e $java ]; then
echo $java
return
fi
done
}
# So far, scylla-jmx only works with Java 11 and Java 8, we prefer the newer
# one for better upstream support.
expected_java_versions="11 1.8"

function select_java_others() {
javaver=$(/usr/bin/java -version 2>&1|head -n1|cut -f 3 -d " ")
if [[ "$javaver" =~ ^\"1.8.0 || "$javaver" =~ ^\"11.0. ]]; then
echo /usr/bin/java
fi
}

. /etc/os-release
case "$ID" in
ubuntu|debian)
java=$(select_java_debian)
;;
fedora|centos)
java=$(select_java_fedora)
;;
esac
if [ -n "$java" ]; then
: # good
elif [ -x /usr/bin/java ]; then
java=$(select_java_others)
fi
java=$(select_java $expected_java_versions)
if [ -z "$java" ]; then
echo "Unable to find java executable" >& 2
exit 1
fi

Expand Down

0 comments on commit 3e62ffe

Please sign in to comment.