Skip to content

Commit 3e299a2

Browse files
ahmubashshirmichaellass
authored andcommitted
Add exec flag to run commands with selected JVM
Allow running arbitrary commands with JAVA_HOME and PATH set to the selected JVM. Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
1 parent d149488 commit 3e299a2

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

archlinux-java-run.sh

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ function print_usage {
3636
USAGE:
3737
archlinux-java-run [-a|--min MIN] [-b|--max MAX] [-p|--package PKG]
3838
[-f|--feature FEATURE] [-h|--help] [-v|--verbose]
39-
[-d|--dry-run] [-j|--java-home]
40-
-- JAVA_ARGS
39+
[-d|--dry-run] [-j|--java-home] [-e|--exec]
40+
-- <JAVA_ARGS | EXEC_ARGS>
4141
4242
EOF
4343
}
@@ -191,6 +191,23 @@ function generate_candiates {
191191
echo "$list" | xargs
192192
}
193193

194+
function env_switch_java_home() {
195+
quote_args
196+
197+
if [ $dryrun -eq 1 ]; then
198+
echo "DRY-RUN - Generated command: env JAVA_HOME=/usr/lib/jvm/${ver} PATH=/usr/lib/jvm/${ver}/bin:\$PATH exec ${quoted_java_args[*]}"
199+
exit 0
200+
fi
201+
202+
if [ $verbose -eq 1 ]; then
203+
echo_stderr "Executing command: JAVA_HOME=/usr/lib/jvm/${ver} PATH=/usr/lib/jvm/${ver}/bin:\$PATH exec ${quoted_java_args[*]}"
204+
fi
205+
206+
export JAVA_HOME="/usr/lib/jvm/${ver}"
207+
export PATH="/usr/lib/jvm/${ver}/bin:$PATH"
208+
exec "${java_args[@]}"
209+
}
210+
194211
function test_javafx_support() {
195212
if [ "$major" -lt 9 ]; then
196213
testcmd="/usr/lib/jvm/${ver}/bin/java -jar ${JAVADIR}/archlinux-java-run/TestJavaFX.jar"
@@ -243,6 +260,7 @@ for arg; do
243260
--min) args+=( -a ) ;;
244261
--max) args+=( -b ) ;;
245262
--help) args+=( -h ) ;;
263+
--exec) args+=( -e ) ;;
246264
--package) args+=( -p ) ;;
247265
--feature) args+=( -f ) ;;
248266
--verbose) args+=( -v ) ;;
@@ -256,6 +274,7 @@ features=( )
256274
java_args=( )
257275
quoted_java_args=( )
258276
verbose=0
277+
exec=0
259278
dryrun=0
260279
javahome=0
261280
args_parsed=0
@@ -307,6 +326,8 @@ while :; do
307326
;;
308327
-j) javahome=1
309328
;;
329+
-e) exec=1
330+
;;
310331
--) args_parsed=1
311332
;;
312333
'') break
@@ -353,6 +374,11 @@ for ver in $candidates; do
353374
exit 0
354375
fi
355376

377+
if [ $exec -eq 1 ]; then
378+
env_switch_java_home
379+
exit 0
380+
fi
381+
356382
for ft in "${features[@]}"; do
357383
case "$ft" in
358384
javafx)

0 commit comments

Comments
 (0)