Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v12
To be released

* Support running arbitrary programs with $JAVA_HOME and $PATH set according
to requested Java version (#11)

## v11
2025-01-03

Expand Down
34 changes: 31 additions & 3 deletions archlinux-java-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# This script uses `exec` on purpose to launch a suitable JRE before the end of
# the script.
# shellcheck disable=SC2093
VERSION=11
VERSION=12
JAVADIR=###JAVADIR###

JAVAFX_MODULES=javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web
Expand All @@ -36,8 +36,8 @@ function print_usage {
USAGE:
archlinux-java-run [-a|--min MIN] [-b|--max MAX] [-p|--package PKG]
[-f|--feature FEATURE] [-h|--help] [-v|--verbose]
[-d|--dry-run] [-j|--java-home]
-- JAVA_ARGS
[-d|--dry-run] [-j|--java-home] [-e|--exec]
-- <JAVA_ARGS | EXEC_ARGS>

EOF
}
Expand Down Expand Up @@ -86,6 +86,8 @@ EXAMPLES:
&& "\$JAVA_HOME"/bin/javac ...
(launches javac from a JDK in version 11 or newer)

archlinux-java-run --min 25 --max 25 --exec -- bash -i
(launches interactive bash with Java 25 set as \$JAVA_HOME and as first element in \$PATH)
EOF
}

Expand Down Expand Up @@ -191,6 +193,23 @@ function generate_candiates {
echo "$list" | xargs
}

function env_switch_java_home() {
quote_args

if [ $dryrun -eq 1 ]; then
echo "DRY-RUN - Generated command: env JAVA_HOME=/usr/lib/jvm/${ver} PATH=/usr/lib/jvm/${ver}/bin:\$PATH exec ${quoted_java_args[*]}"
exit 0
fi

if [ $verbose -eq 1 ]; then
echo_stderr "Executing command: JAVA_HOME=/usr/lib/jvm/${ver} PATH=/usr/lib/jvm/${ver}/bin:\$PATH exec ${quoted_java_args[*]}"
fi

export JAVA_HOME="/usr/lib/jvm/${ver}"
export PATH="/usr/lib/jvm/${ver}/bin:$PATH"
exec "${java_args[@]}"
}

function test_javafx_support() {
if [ "$major" -lt 9 ]; then
testcmd="/usr/lib/jvm/${ver}/bin/java -jar ${JAVADIR}/archlinux-java-run/TestJavaFX.jar"
Expand Down Expand Up @@ -243,6 +262,7 @@ for arg; do
--min) args+=( -a ) ;;
--max) args+=( -b ) ;;
--help) args+=( -h ) ;;
--exec) args+=( -e ) ;;
--package) args+=( -p ) ;;
--feature) args+=( -f ) ;;
--verbose) args+=( -v ) ;;
Expand All @@ -256,6 +276,7 @@ features=( )
java_args=( )
quoted_java_args=( )
verbose=0
exec=0
dryrun=0
javahome=0
args_parsed=0
Expand Down Expand Up @@ -307,6 +328,8 @@ while :; do
;;
-j) javahome=1
;;
-e) exec=1
;;
--) args_parsed=1
;;
'') break
Expand Down Expand Up @@ -353,6 +376,11 @@ for ver in $candidates; do
exit 0
fi

if [ $exec -eq 1 ]; then
env_switch_java_home
exit 0
fi

for ft in "${features[@]}"; do
case "$ft" in
javafx)
Expand Down