Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8341391

cannot run a java shebang script starting with package stmt from /usr/bin

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Ubuntu 22.04 jammy, but it will happen under any Linux

      A DESCRIPTION OF THE PROBLEM :
      If a java shebang script contains a `package x.y.z;` statement, you cannot run it from just any directory, as it used to be in Java 21. You get an error like:
      ```
      $ ./hello
      error: end of path to source file does not match its package name org.example: ./hello
      ```

      REGRESSION : Last worked in version 21.0.4

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create the file hello:
      ```
      #!/usr/lib/jvm/jdk-23-oracle-x64/bin/java --source 11
      //#!/usr/bin/java --source 11

      package org.example;

      public class Main {
          public static void main(String... a) {
              System.out.println("hello");
          }
      }
      ```
      2. chmod a+x hello
      3. [optional] move hello to /usr/bin
      4. run this script, e.g. ./hello


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      the script runs
      ACTUAL -
      error: end of path to source file does not match its package name org.example

      ---------- BEGIN SOURCE ----------
      #!/usr/lib/jvm/jdk-23-oracle-x64/bin/java --source 11
      //#!/usr/bin/java --source 11

      package org.example;

      public class Main {
          public static void main(String... a) {
              System.out.println("hello");
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Create an additional script, and create the directories corresponding to the package:

      mkdir -p org/example
      mv hello org/example/
      echo -e '#!/bin/bash\n'`pwd`'/org/example/hello'>hello
      chmod a+x hello
      ./hello

      Probably changing the package name to bin.usr would also work ;)


      FREQUENCY : always


            tongwan Andrew Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: