by whartung · 2y ago · 0 comments
I don't know how many folks know, although its been there for a bit, that you no longer have to specifically compile single file java programs.
You can just run them with with "java prog.java".
(Honestly, I haven't used the technique myself, still old school -- even with silly small x.java tests and such. Muscle memory and all that.)
$ ./shebang.java
./shebang.java:1: error: illegal character: '#'
#!/usr/bin/java
^
./shebang.java:1: error: class, interface, enum, or record expected
#!/usr/bin/java
^
2 errors
error: compilation failed
Self-executable jars though, that's an interesting idea. No reason why it can't work, zip archives are read from the end, you can put anything in the beginning, including "#!/usr/bin/java -jar" java -jar $0
The rest of it sets up the environment (JAVA_OPTS, -Xmx, etc).