Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sadly you can't, Java doesn't support comments starting with #

    $ ./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"


I believe you can use shebang since java 11 but you can't have the filename extension be .java.



this has worked since the beginning but i don't think many people know about it


For the self executing JAR, I normally add a bash/shell stub for the header that ends with

    java -jar $0
The rest of it sets up the environment (JAVA_OPTS, -Xmx, etc).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: