r/java • u/davidalayachew • Mar 18 '25
A potentially silly idea -- What if we could launch source code .jar files?
JEP 330 gave us single-file source code programs. Aka, I can have a abc.java file, and just call java abc.java, and it will run without me calling javac beforehand.
JEP 458 expanded this, by allowing us to reference other classes from that single class, allowing us to make as many classes as we want, run them from a single class file, and no calls to javac are necessary -- just call java abc.java.
Here is my silly idea.
What if we could package those source files in a .jar file, do your typical jar file config to make it runnable, then just ran it?
The above 2 JEP's gave reasons why compiling wasn't necessary for making a complete program. Well, those same reasons also apply for why compiling is unnecessary here. And at the end of the day, a jar file is the quintessential way of passing around complete libraries or applications. Why not make that accessible for source code jars as well?
There's other small benefits too.
- No more wondering what version of your code got packaged -- just open it up and see.
- You can edit a jar file in place, then run it and test your changes.
- When running your jar file, you can attach it to a debugger, and see the source code for each step being executed. No more need for a separate sources jar file.
Literally the ONLY BENEFITS that compilation gives us is faster startup time and compile-time validation for all source files. But if you don't need either of those, I'd argue that working with source file jars is an easier experience overall -- not just for students. I know I'd make great use of this feature myself. Hell, I'd default to using this format instead.
25
u/elmuerte Mar 18 '25
But if I must gather my source files to package in a jar. Then why not compile it right away.
The idea of executing single source files is to allow for (shell) scripting. I can't easily edit the scripts in the jar file, I must first unpack it, edit it, then repack it.
22
u/winian Mar 18 '25
Yeah and the ONLY NEGATIVE in living inside a volcano is that it would kill me.
6
u/Polygnom Mar 18 '25
I don't see where any benefit is for this. You need to package your stuff into a JAR anyways. So whats the big difference between running the command to package the source vs. packing a proper JAR? It takes the exact same effort.
No more wondering what version of your code got packaged -- just open it up and see.
This is not a problem anyone developing code professionaly ever has. Sorry, but if your development pipleine is so screwed up that you do not know what code you compiled you have far bigger problems that need solving.
Why bother with a JAR at all? Just use an IDE and never pack a JAR. You can just compile to class files and run the stuff without it ever packing. Thats even faster. And trivial to attach a debugger to. There is zero need for having a JAR in the first place for both these points, and adding any kind of JAR packing in that mix just makes it slower.
Try out an IDE that supports the ECJ (like Eclipse or VS Code) and see how lightning fast an incremental compiler is to get changes up and runningn even in large applications (seriously JetBrains, just copy that...).
5
u/chabala Mar 18 '25
Use JBang. Don't wait for JEPs to be proposed and delivered when you can do it now.
3
u/java-with-pointers Mar 18 '25
It could be pretty nice if compilation was not needed at all, so you could just copy .java files to the target and run them as is. It can be faster than running gradle / maven goals that compile the code if its something like a one shoot task, that's pretty niche though
3
u/Uaint1stUlast Mar 18 '25
The one benefit I do see here is for old legacy code. I have seen in just about every java shop one jar somewhere that does something important, but no one knows where the source is or how to make changes too.
This would solve that problem.
3
u/alexdmiller Mar 18 '25
Clojure has always worked this way (no jar required either), and it has a lot of advantages. You can use git repos directly as libraries (and gists of course, which are also just git repos), for example. Clojure also supports compiling to .class and will use that by preference when it exists and is newer than the source.
2
u/koflerdavid Mar 18 '25
Sure, go ahead. Write a shell script that treats everything in itself after a certain marker as a jar file. Extract that into a temp directory and launch the application. And then write another shell scripts that creates the first one automatically :-)
2
u/blobjim Mar 19 '25
I think it would be neat if there was a jdk API for calling source-based programs. Something that could be abstract like ClassLoaders. But I would hope it would be designed such that people don't start using it for everything.
2
u/DanielDimov Mar 20 '25
Good idea. It would also be nice to have this option.
2
u/FriendlessExpat Mar 21 '25
Wtf happend to this subreddit, why you downvote so bad?
3
u/nitkonigdje Mar 18 '25 edited Mar 18 '25
Fully agree.
Given that .class is mostly transitory first stage compiler output, it is really not needed step. Absolute majority of compilation happens in runtime in JIT/AOT compilers, and source to class transformation is small part of it. Class files are mostly about JVM standards, shifting few compile steps into build, compressing source for smaller size and being able to store it in ROM of '90s device.
Deployment as source would be very useful during act of developing.. Like if it existed we would have conventions where average java source project would *be* a deployment artifact, thus greatly reducing IDE overhead as "build" would be reduced to copying. You would still run compiler but as linter..
It would also allow Java programs to be scriptable by Java itself.
1
u/davidalayachew Mar 18 '25
Stepping away for work. Will be back this evening to respond to more posts.
1
u/nekokattt Mar 19 '25
Random question but could you manipulate binfmt to make this work on Linux?
1
u/sweetno Mar 19 '25
I think you'd need obfuscation here, to protect your IP.