JDK-8341391 : cannot run a java shebang script starting with package stmt from /usr/bin
  • Type: Bug
  • Component: tools
  • Sub-Component: launcher
  • Affected Version: 22
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2024-10-01
  • Updated: 2024-10-02
  • Resolved: 2024-10-02
Related Reports
Relates :  
Description
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



Comments
This is a new JEP introduced in JDK 22: https://openjdk.org/jeps/458 Closed as not an issue.
02-10-2024