Skip to content
Merged
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
2026-02-20

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

## v11
2025-01-03

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
-- <JAVA_ARGS | EXEC_CMD>
```

## Available features
Expand All @@ -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`
39 changes: 34 additions & 5 deletions archlinux-java-run.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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_CMD>

EOF
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 ) ;;
Expand All @@ -256,6 +278,7 @@ features=( )
java_args=( )
quoted_java_args=( )
verbose=0
exec=0
dryrun=0
javahome=0
args_parsed=0
Expand Down Expand Up @@ -307,6 +330,8 @@ while :; do
;;
-j) javahome=1
;;
-e) exec=1
;;
--) args_parsed=1
;;
'') break
Expand Down Expand Up @@ -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)
Expand Down