Re:1JPM: A Maven/Gradle alternative in a single Java file
by grishka on Monday July 08, 2024 @08:03PM
(#40911145)
I do it a lot. These single-file programs can be called Java scripts when run without compilation.
[ Parent
| on HN ]
Re:1JPM: A Maven/Gradle alternative in a single Java file
by apantel on Monday July 08, 2024 @08:55PM
(#40911509)
I wonder what could be done with Java script…
Re:1JPM: A Maven/Gradle alternative in a single Java file
by bionsystem on Tuesday July 09, 2024 @03:53AM
(#40913560)
Sounds like a name that could have some success. Given that the logo for Java is a cup of coffee, maybe we could also call it "Coffee script".
Re:1JPM: A Maven/Gradle alternative in a single Java file
by mbreese on Monday July 08, 2024 @10:02PM
(#40911860)
Can you add a shebang line and have it truly be a script? I do self executing JARs with a bash stub/header all the time, but haven’t tried with this.
Re:1JPM: A Maven/Gradle alternative in a single Java file
by grishka on Monday July 08, 2024 @10:08PM
(#40911893)
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"
Re:1JPM: A Maven/Gradle alternative in a single Java file
by mbreese on Monday July 08, 2024 @10:55PM
(#40912115)
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).
Re:1JPM: A Maven/Gradle alternative in a single Java file
by jnellis on Monday July 08, 2024 @10:24PM
(#40911966)
I believe you can use shebang since java 11 but you can't have the filename extension be .java.
Re:1JPM: A Maven/Gradle alternative in a single Java file
by grishka on Tuesday July 09, 2024 @12:06AM
(#40912453)
It turns out you indeed can: https://news.ycombinator.com/item?id=40912444
Re:1JPM: A Maven/Gradle alternative in a single Java file
by spullara on Monday July 08, 2024 @10:20PM
(#40911946)
this has worked since the beginning but i don't think many people know about it
Re:1JPM: A Maven/Gradle alternative in a single Java file
by metadat on Monday July 08, 2024 @11:48PM
(#40912377)
How do you run a Java program without compilation?
Re:1JPM: A Maven/Gradle alternative in a single Java file
by grishka on Monday July 08, 2024 @11:56PM
(#40912408)
It technically compiles it to memory before running it, but the point is that you don't have to invoke javac first. It's a relatively new feature.
Re:1JPM: A Maven/Gradle alternative in a single Java file
by metadat on Monday July 08, 2024 @11:58PM
(#40912419)
Do you know for which Java version this first became available?
Re:1JPM: A Maven/Gradle alternative in a single Java file
by grishka on Tuesday July 09, 2024 @12:03AM
(#40912444)
11: https://openjdk.org/jeps/330
Oh so actually, you can use a shebang, just without the .java extension and with the "--source" flag, it's specifically written out in that spec. I was doing my experiment wrong!