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.

Fixes scylladb#212
Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
  • Loading branch information
tchaikov committed May 15, 2023
1 parent e23b12c commit 7075fc2
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions scripts/select-java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ function select_java_fedora() {
}

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
local java
local javaver
java=$1
shift
javaver=$($java -XshowSettings:properties -version 2>&1 | awk -F' = ' '/java.specification.version/ {print $NF}')
case "$javaver" in
1.8|11)
echo $java
;;
*)
;;
esac
}

. /etc/os-release
Expand All @@ -52,7 +60,7 @@ done
if [ -n "$java" ]; then
: # good
elif [ -x /usr/bin/java ]; then
java=$(select_java_others)
java=$(select_java_others /usr/bin/java)
fi
if [ -z "$java" ]; then
exit 1
Expand Down

0 comments on commit 7075fc2

Please sign in to comment.