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.
6
u/suspiciously_calm Jul 05 '15
AFAIK the zip "header" is actually a zip "footer" with negative pointers from the end of the file, so the prepended sheband doesn't interfere with the package contents.
2
u/mck1117 Jul 05 '15
Just like in windows how you can put a WinPE header and a little bit of code at the beginning of a JAR to make it executable as an EXE.
4
u/notjfd Jul 05 '15 edited Jul 05 '15
There's another way to make java programs start without explicitly calling the runtime, and that's by using a kernel feature called linux_binfmt. As some of you might know, the shebang is actually a so-called magic number. When executing a file, the loader looks at the first few bits of the file, which are usually unique for many file types. If it corresponds with the ELF binary format (linux's "exe"), it will call ld-linux. If it corresponds with the ASCII sequence for the shebang, it will instead parse the next few bytes after it and interpret it as the parser (like python, sh, or other popular script parsers).
The exciting part, however, is that you can add additional definitions to this magic table. This is where linux_binfmt comes in. It exposes a kernel interface which allows you to feed it magic numbers and their corresponding runtime. Two of the most popular uses of these are Java and .NET executables (which have the .exe extension but are not in the win32 binary format and thus have another magic number), which can respectively be opened with the JRE and Mono.
5
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
6
u/kageurufu Jul 05 '15
Its really that zip file headers are not required to start the file.
cat image.jpg file.zip > hiddenzip.jpg
It will open in a zip program, but look like a jpg.
I have a huge folder of book cover images that have ebooks stored this way
4
u/sysop073 Jul 05 '15
I believe you, but I don't think it has anything to do with what I said. I said that if OP's understanding were true and Java were actually ignoring that shebang, that doesn't seem like a "programming horror", it seems like a pretty useful thing for them to have implemented. It ended up not even being right, but I disagree with the post either way; that'd be a pretty useful feature in my opinion
2
1
u/TWiStErRob Feb 17 '24
Why does this not work now?
$ ./hello
./hello: line 2: $'PK\003\004\024\b\b\b%AQX': command not found
./hello: line 3: syntax error near unexpected token `('
./hello: line 3: `E-4BJJJ5y|3ts<RsryxPwk&IJPK%AQXHello.classmPMK@}$MS[[S? DsP"BD6RCb"9zPG({yfp:ZXicU~4oIhT"/8#FXF3(H$S?3LE6g$2ÒDĚulU`6͢Ħ-l'diagnostics if relevant:
$ uname -a
Linux my-pc 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ java -version
openjdk version "11.0.19" 2023-04-18
OpenJDK Runtime Environment (build 11.0.19+7-post-Ubuntu-0ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.19+7-post-Ubuntu-0ubuntu118.04.1, mixed mode, sharing)1
u/TWiStErRob Feb 17 '24
Ok, this seems to work: https://www.reddit.com/r/java/comments/i4zpj/making_really_executable_jars/
echo -e "#\!/bin/sh\nexec java -jar \$0 \"\$@\"" > hellobut I don't like that it's not using
env javaand assumes sh.
0
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
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 toscript) — and how else would#!/usr/bin/pythonwork?
45
u/wung Jul 04 '15 edited Jul 04 '15
That's not a java thing, but rather just your
shellprogram loader.If you start something with ./, it will try to figure out how to.It will figure out how to execute the program by checking out the header. If it begins with an elf-header, it will start the binary. If it starts with a shebang, it will start it using the given command, passing the file to it:The fact that java does not crash with the shebang there, comes from the way zip files are made: the header is at the end of the file.