jbang icon indicating copy to clipboard operation
jbang copied to clipboard

Support running via Single-File Java Launcher (JEP 330) ?

Open foreverigor opened this issue 1 year ago • 5 comments

Is your feature request related to a problem? Please describe. JBang is great because it allows you to specify dependencies right in your script file. However for cases where dependencies are not needed it will still build a jar from the source. Since Java 11 the java launcher has the ability to launch such java source files directly: JEP 330: Launch Single-File Source-Code Programs

Describe the solution you'd like When having access to a Java 11+ jvm, make the distinction for scripts which doesn't require any jbang-exclusive features and launch these directly with the java launcher, skipping the build step. Some tags obviously require the jar to be built (anything involving dependencies or other sources), but others can be directly mapped to java options, like //JAVA, //RUNTIME_OPTIONS, some //COMPILE_OPTIONS

Describe alternatives you've considered It's actually possible to run jbang scripts via the java launcher directly, but of course none of the jbang tags will be picked up

Additional context Launching compatible source files directly has numerous advantages: We're saving one JVM launch and reduce first-time run times considerably. We also don't have to create a jar so we're skipping unnecessary disk writes & File I/O.

foreverigor avatar Apr 04 '24 14:04 foreverigor

Not sure it will make that big diff.

Are you saying jbang/java X.jar is noticably slower than java X.java?

maxandersen avatar Apr 04 '24 15:04 maxandersen

No, not running the jar. But the jar build process is what's slower. So, ./X.java vs java X.java. It's noticeable if you edit the script a lot

foreverigor avatar Apr 04 '24 16:04 foreverigor

Yes I get that - but most will run not build.

Not having jar also means can't store which main methods to use or run any integration code (IE. Quarkus or jbang)

I'm not sure we can automatic detect if it's feasible - probably need an explicit flag.

//NOJAR ?

maxandersen avatar Apr 04 '24 20:04 maxandersen

Too me this seems pretty fragile, we'd suddenly get possibly different behaviours depending on what code you're running (does it have dependencies or not?) and which JDK you're using (<11? compile to jar. 11? no jar, but only single file, 22? multiple files). I think that up to now we've tried hard to get the same behaviour everywhere across the board, so my first reaction at least would be -1. BUt I'm open to being convinced :-)

quintesse avatar Apr 04 '24 20:04 quintesse

I thought it was possible to just create a list of tags – those for which a jar is required and not. At least that was my idea when I glanced at the code.

different behaviours

Yes, that's also occured to me. Currently everything goes through the build jar -> run pipeline (except for jsh). Not sure how cleanly this branching could be implemented. I'll see what I can do myself

foreverigor avatar Apr 04 '24 20:04 foreverigor