diff --git a/CHANGES.md b/CHANGES.md index 09c0338..707ea4c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,11 @@ # Changelog +## v12 +2026-02-20 + +* Support running arbitrary programs with $JAVA_HOME and $PATH set according + to requested Java version (#11, #12) + ## v11 2025-01-03 diff --git a/LICENSE b/LICENSE index 622cb2b..55e1269 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2017, 2018, 2019, 2020, 2023, 2024 Michael Lass +Copyright (c) 2017–2026 Michael Lass Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index b0a31d0..5393e12 100644 --- a/README.md +++ b/README.md @@ -12,14 +12,16 @@ this version, the one corresponding to the user's default JVM is used. By default, archlinux-java-run will execute a suitable version of java with the given JAVA_ARGS. When run with -j|--java-home, it just prints the location of a suitable java installation so that custom commands -can be run. +can be run. When run with -e|--exec, it will run EXEC_CMD in an +environment where $JAVA_HOME and $PATH is set so that the appropriate +Java version is used. ## 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] + -- ``` ## Available features @@ -45,3 +47,6 @@ can be run. * Launch javac from a JDK in version 11 or newer: `JAVA_HOME=$(archlinux-java-run --min 11 --feature jdk --java-home) && "$JAVA_HOME"/bin/javac ...` + +* Launch interactive bash with Java 25 set as $JAVA_HOME and as first element in $PATH: + `archlinux-java-run --min 25 --max 25 --exec -- bash -i` diff --git a/archlinux-java-run.sh b/archlinux-java-run.sh index 10c0f6f..cd27580 100755 --- a/archlinux-java-run.sh +++ b/archlinux-java-run.sh @@ -1,7 +1,7 @@ #!/bin/bash # -# (c) 2017, 2018, 2019, 2020, 2023, 2024 Michael Lass +# Copyright (c) 2017–2026 Michael Lass # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -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 @@ -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] + -- EOF } @@ -58,7 +58,9 @@ this version, the one corresponding to the user's default JVM is used. By default, archlinux-java-run will execute a suitable version of java with the given JAVA_ARGS. When run with -j|--java-home, it just prints the location of a suitable java installation so that custom commands -can be run. +can be run. When run with -e|--exec, it will run EXEC_CMD in an +environment where \$JAVA_HOME and \$PATH is set so that the appropriate +Java version is used. EOF print_usage cat << EOF @@ -86,6 +88,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 } @@ -191,6 +195,23 @@ function generate_candiates { echo "$list" | xargs } +function exec_in_modified_env() { + 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" @@ -243,6 +264,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 ) ;; @@ -256,6 +278,7 @@ features=( ) java_args=( ) quoted_java_args=( ) verbose=0 +exec=0 dryrun=0 javahome=0 args_parsed=0 @@ -307,6 +330,8 @@ while :; do ;; -j) javahome=1 ;; + -e) exec=1 + ;; --) args_parsed=1 ;; '') break @@ -353,6 +378,10 @@ for ver in $candidates; do exit 0 fi + if [ $exec -eq 1 ]; then + exec_in_modified_env + fi + for ft in "${features[@]}"; do case "$ft" in javafx)