I was wondering about the potential of launching source code directly from .jar files. With the introduction of JEP 330 and JEP 458, Java now allows us to run single-file programs without needing to compile them first. What if we could take this a step further and package our source files in a .jar file, configure it to be runnable, and execute it directly? This way, we wouldn't need to worry about compiling separately. It would simplify the process of passing around complete libraries or applications in source code form. I'm curious about the upsides and downsides of this approach and whether it could be beneficial for both beginners and professionals.
5 Answers
The one time I see this as useful is with old legacy code that's in jars without any source. Having the source available in the jar would solve a lot of problems where no one knows how to modify things.
Exactly! Even for new developers, having easy access to the source can help them understand what's happening without digging through compiled classes.
That's an interesting thought! But if you're already gathering your source files to make a jar, why not compile them right away? The idea of executing single files is good for scripting, and editing scripts inside a jar could be a hassle since you'd have to unpack and repack it each time. It just seems more practical to compile straight away, you know?
I see your point, but debugging is easier with source jars. Adapting a debugger directly to a runnable jar could simplify things, since you'd have direct access to the source—no need for a separate source jar.
But for shell scripting, Java isn't the go-to. The startup time can be a killer unless you have native compilation.
Clojure has already been doing something similar, without jars. Its flexibility allows you to directly work with source files, and even use git repos as libraries. It's a great model for simplifying workflow.
Intriguing! I know some about Clojure; its syntax feels nice with my Lisp background. What keeps you using it compared to Java?
Clojure being on JVM makes it appealing. I've been diving into Java features lately, but Clojure's approach piques my interest.
Honestly, I'm not convinced this idea has any real benefits. You're already packing things into a jar, so what's the difference? The effort to package sources isn't much different from compiling. Plus, it's uncommon to be unsure of which version you've packaged if you're working professionally. It often falls on the developer to manage their patches well—something that should be sorted out without relying on this kind of approach.
I get what you're saying, but many students struggle with knowing which version of their code is active. For them, a source jar could clear up that confusion quickly.
But why bother with jars at all? Just code directly in an IDE and run from there. Compiling to class files is quicker without the jar overhead.
Stepping back from Java for a bit, is there a way to manipulate binfmt on Linux to allow this? I'm curious if this can be done at a systems level.
I'm actually not familiar with that, but it sounds interesting!
You could experiment; it might work in theory! Might need some clever scripting.
Jars can always be decompiled, but you might lose some info. Having source in the jar just makes life easier.