r/programminghorror • • Jul 04 '15

Yes, Java supports shebangs.

Let Hello.java be the following:

public class Hello {
    public static void main(String... args) {
        System.out.println("Hello, World!");
    }
}

Then, running the following shell commands:

javac Hello.java
jar -cfe Hello.jar Hello Hello.class
echo "#\!/usr/bin/env java -jar" > hello
cat Hello.jar >> hello
chmod +x hello
./hello

will produce the output "Hello, World!". Java will ignore the shebang when parsing the jar, until it hits the zip header.

Wat.

0 Upvotes

15 comments sorted by

View all comments

0

u/[deleted] Jul 05 '15

[deleted]

1

u/ActuallyRuben Jul 05 '15

But that defies the complete purpose of this post about using a shebang to run the jar

0

u/[deleted] Jul 05 '15

[deleted]

3

u/Kwpolska Jul 06 '15

The shebang line is not passed to the interpreter; rather, the filename is. You are actually running /usr/bin/cat test.txt script (you can tell if you add one more line to script) — and how else would #!/usr/bin/python work?