r/programminghorror • u/Xor_Boole • 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
./hellowill produce the output "Hello, World!". Java will ignore the shebang when parsing the jar, until it hits the zip header.
Wat.
0
Upvotes
2
u/sysop073 Jul 04 '15
If Java actually had anything to do with ignoring the shebang, I would call that a useful feature; why would that be bad? The syntax is chosen because
#is a comment in lots of languages, but for programs that would otherwise choke on that line it's nice of them to special case it so shebangs will still work