Skip to content

Support direct exec with proper env set #11

Closed

Description

@ahmubashshir
diff --git a/archlinux-java-run.sh b/archlinux-java-run.sh
index 10c0f6f..22aab76 100755
--- a/archlinux-java-run.sh
+++ b/archlinux-java-run.sh
@@ -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
 }
@@ -191,6 +191,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"
@@ -243,6 +260,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 +274,7 @@ features=( )
 java_args=( )
 quoted_java_args=( )
 verbose=0
+exec=0
 dryrun=0
 javahome=0
 args_parsed=0
@@ -307,6 +326,8 @@ while :; do
         ;;
     -j) javahome=1
         ;;
+    -e) exec=1
+        ;;
     --) args_parsed=1
         ;;
     '') break
@@ -353,6 +374,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)

Activity

  1. michaellass commented on Feb 14, 2026

    @michaellass
    Owner

    Hi! Is there a specific reason you posted this change as an issue instead of a pull request?

  2. ahmubashshir commented on Feb 14, 2026

    @ahmubashshir
    ContributorAuthor

    Didn't want to create yet another fork that I might delete after the PR gets merged...

    It'd be great if github could create PR's from email (like source hut)

    or, I can paste git-am'able patch if you want...

    (actually I'm trying to convince myself that gerrit is a good idea, sent a few patches via gerrit so... /s)

  3. michaellass commented on Feb 14, 2026

    @michaellass
    Owner

    A patch file would be great. That way, there is a commit description and it states you as the author.

    Some questions on the change itself:

    • The default behavior of archlinux-java-run is to use exec to call Java. So it seems to me that the main addition of the newly added env_switch_java_home function is to set $JAVA_HOME and $PATH before doing the exec. Is that right? If so, exec seems not the most descriptive command line flag to trigger this behavior.
    • Is it deliberate to call env_switch_java_home before the loop that adds feature-specific Java args?
  4. ahmubashshir commented on Feb 14, 2026

    @ahmubashshir
    ContributorAuthor

    As this executes arbitrary executable, instead of $JAVA_HOME/bin/java, I think it should be called before feature-specific java args...

    I'll primarily use it to spawn a shell with non-default jdk to make gradle run without freaking out...

    like this archlinux-java-run -b 11 -f jdk -e -- bash -i

  5. ahmubashshir commented on Feb 14, 2026

    @ahmubashshir
    ContributorAuthor

    or this flow will be better?

    diff --git a/archlinux-java-run.sh b/archlinux-java-run.sh
    index 10c0f6f..d3343b6 100755
    --- a/archlinux-java-run.sh
    +++ b/archlinux-java-run.sh
    @@ -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
     }
    @@ -243,6 +243,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 +257,7 @@ features=( )
     java_args=( )
     quoted_java_args=( )
     verbose=0
    +exec=0
     dryrun=0
     javahome=0
     args_parsed=0
    @@ -307,6 +309,8 @@ while :; do
             ;;
         -j) javahome=1
             ;;
    +    -e) exec=1
    +        ;;
         --) args_parsed=1
             ;;
         '') break
    @@ -353,31 +357,36 @@ for ver in $candidates; do
         exit 0
       fi
     
    -  for ft in "${features[@]}"; do
    -    case "$ft" in
    -    javafx)
    -      if [ "$major" -gt 8 ]; then
    -        echo_stderr "Modifying java arguments to support system installation of JavaFX"
    -        additional_mpath=$(eval echo "/usr/lib/jvm/{${ver},java-${major}-openjfx}/lib/{javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web,javafx-swt}.jar" | tr ' ' :)
    -        extend_java_args module-path "$additional_mpath" ':'
    -        extend_java_args add-modules "$JAVAFX_MODULES" ','
    -      fi
    -      ;;
    -    esac
    -  done
    +  if [ $exec -eq 0 ]; then
    +    for ft in "${features[@]}"; do
    +      case "$ft" in
    +      javafx)
    +        if [ "$major" -gt 8 ]; then
    +          echo_stderr "Modifying java arguments to support system installation of JavaFX"
    +          additional_mpath=$(eval echo "/usr/lib/jvm/{${ver},java-${major}-openjfx}/lib/{javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web,javafx-swt}.jar" | tr ' ' :)
    +          extend_java_args module-path "$additional_mpath" ':'
    +          extend_java_args add-modules "$JAVAFX_MODULES" ','
    +        fi
    +        ;;
    +      esac
    +    done
    +    java_args=("/usr/lib/jvm/${ver}/bin/java" "${java_args[@]}")
    +  else
    +    java_args=("env" "JAVA_HOME=/usr/lib/jvm/${ver}" "PATH=/usr/lib/jvm/${ver}/bin:$PATH" "${java_args[@]}")
    +  fi
     
       quote_args
     
       if [ $dryrun -eq 1 ]; then
    -    echo "DRY-RUN - Generated command: /usr/lib/jvm/${ver}/bin/java ${quoted_java_args[*]}"
    +    echo "DRY-RUN - Generated command: ${quoted_java_args[*]}"
         exit 0
       fi
     
       if [ $verbose -eq 1 ]; then
    -    echo_stderr "Executing command: /usr/lib/jvm/${ver}/bin/java ${quoted_java_args[*]}"
    +    echo_stderr "Executing command: ${quoted_java_args[*]}"
       fi
     
    -  exec /usr/lib/jvm/"$ver"/bin/java "${java_args[@]}"
    +  exec "${java_args[@]}"
     
     done

    I tried minimizing line changes in the first patch...

  6. michaellass commented on Feb 14, 2026

    @michaellass
    Owner

    Ah, I missed that. OK, that is indeed a use case that I already needed in a package and implemented directly in the package. But having it in here does make sense.

    Both versions are fine. I think I prefer the first one as it has less deep nesting. Could you post a git am patch?

  7. ahmubashshir commented on Feb 14, 2026

    @ahmubashshir
    ContributorAuthor
  8. michaellass commented on Feb 14, 2026

    @michaellass
    Owner

    Thanks for your contribution! I will likely release this as v12 soon (see #12)

  9. michaellass commented on Feb 20, 2026

    @michaellass
    Owner

    The change has been merged by now. v12 including this addition will be released within the next couple of minutes. Thanks again for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions