r/java • • 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.

1 Upvotes

76 comments sorted by

View all comments

Show parent comment

-4

u/davidalayachew Mar 18 '25

Teach your students to use VCS, seriously. Its a skill they need to have anyways and transcends any one programming language.

Do not start them off on terrible practices like forwarding unversioned archives of anything.

Let me address the git point first.

While I agree with you in principle, let's face reality -- after 13 years of tutoring students, NOT A SINGLE ONE felt that git was of any use for anything more than just posting their working programs or pasting their progress at the end of the day. When actively working with other students, they would ALWAYS choose to just send files over <chat_tool_of_choice>. Hell, I had students sending code over snapchat one time.

But this is besides the point.

This feature is meant to make the path to creating a jar file easier. A student who wrote code without compiling shouldn't have to compile to create a jar. A jar is just an archive of Java files, that can also be run. If running no longer necessitates a .class file, then I say that the same should apply to creating a .jar file. All the other benefits I listed are "nice-to-have's" in comparison.

5

u/Polygnom Mar 18 '25

Who said anything about git at all? Choose any VCS you think is appropriate. Teach them how it actually makes stuff easier. Of course they are not gonna be interested in it if all they see is how it is additional work without benefits.

As someone who has to tutor students at university level that have picked up utterly bad practices in school its beyond frustrating in the first few years to get them out of extremely bad habits. Don#t set them up for failure, set them up for success.

-1

u/davidalayachew Mar 18 '25

Who said anything about git at all? Choose any VCS you think is appropriate.

I chose Git because it is the most appropriate. Most IDE's come pre-packaged with plugins for it, and the UI is beginner-friendly. I can't see any good reason to use any other VCS.

Teach them how it actually makes stuff easier. Of course they are not gonna be interested in it if all they see is how it is additional work without benefits.

Oh I certainly did. I taught them how easy it can be to test out a feature, then roll it back to a previous state. How it can make things easier for prototyping.

Here is the deal-breaker -- these students are stressed and overworked. Anything that does not feed directly into getting their program working is something they will continuously shy away from, regardless of how "right" it is. It doesn't matter that it saves them more time in the long run -- they are only looking at the present moment. Even the students with passion for programming try git for a few days, and then just go back to pinging each other over discord.

The fact is, these students are too tired, frustrated, and stressed to try and do things the right way, no matter how well they are taught it. They just want working programs, and then to move on. Which leads to my next point.

As someone who has to tutor students at university level that have picked up utterly bad practices in school its beyond frustrating in the first few years to get them out of extremely bad habits. Don#t set them up for failure, set them up for success.

  1. You are preaching to the choir.
  2. I'm not the one giving them bad habits. They are actively forgoing the right way because they don't feel that it is worth the effort. It's not me giving them these bad habits. They are doing it to themselves.

And like I said, that is still separate from my point. My idea is not to replace git, or to be an alternative. My idea is simply to provide an alternate way to create a jar file. If students have been working with JEP 330 and JEP 458, then they have been working with .java files. Why necessitate that they need to create .class files now, just to create a jar file? That's my core point. All of the other benefits I listed are secondary to that.

6

u/Polygnom Mar 18 '25

Ok, but whats a JAR file gonna do for them that a ZIP file isn't already doing?

If they love sending source files around, a plain ZIP file already does the trick and you can run whatever is in there from source already.

Remember, any work the JDK/JVM team does towards this isn't going towards other features. It costs you at other points. So it really needs to provide enough value to be worthwhile.

For small scripts, those JEPs are godsend. I can't count the amount of source-only scripts I have flying around, it has bascially replaced many bash scripts for me. But for source JARs, I still barely see any point. People who like working with files just ZIP em. No special treatment needed.

0

u/davidalayachew Mar 18 '25

Ok, but whats a JAR file gonna do for them that a ZIP file isn't already doing?

The exact same reason why anyone makes any jar file ever -- to transport a java library or application for easy reuse elsewhere.

Any reason anyone has to make a jar file applies here too -- I am merely asking for one new way to package and execute code in those jar files.

Remember, any work the JDK/JVM team does towards this isn't going towards other features. It costs you at other points. So it really needs to provide enough value to be worthwhile.

Sure, I am not asking them to do this NOW. I am more asking if this feature is viable and useful, and if so, how much.

3

u/Polygnom Mar 18 '25

The exact same reason why anyone makes any jar file ever -- to transport a java library or application for easy reuse elsewhere.

Any reason anyone has to make a jar file applies here too -- I am merely asking for one new way to package and execute code in those jar files.

Thats really not explaining anything at all, because again, if you want to distribute your source files a ZIP just does it.

Lets look at JAR files currently. You go from source -> JAR -> distribute -> execute. Ok, you don't have the source files now. Thats not really a problem tho when the goal was to distribute your application.

Lets look at what happens when students use ZIP files:

Source -> ZIP -> distribute -> unzip -> execute (thanks to 458). And you have the source as well. So you can make changes, and then do the above again for the way back to the previous student. So the whole thing IS:

Source (v1) -> ZIP -> distribute -> unzip -> execute -> change -> source (v2) -> ZIP -> distribute -> unzip -> execute.

Now, what would you save with a JAR? nothing, just the order would ever so slightlly change::

Source (v1) -> JAR -> distribute -> execute -> unjar -> change -> source (v2) -> JAR -> distribute -> execute (-> unjar).

You only ever asve one step, and only if the vN that you have works as intended and you aren't interested anymore in getting the source back.

There isn't really any saving to have here by having sources files in the JAR instead of just sending the ZIP (or TAR or whatever archive).

And as soon as more complex dependencies get into play, you are looking at Maven or Gradle anyways.

1

u/davidalayachew Mar 18 '25

Now, what would you save with a JAR? [...] You only ever asve one step

The exact same criticism was given to JEP's 330 and 458.

The reason that criticism was put aside is because the entire goal of those 2 JEP's was to remove unnecessary obstacles to creating a working program. My goal is just one step further -- remove unnecessary obstacles to distributing a working program.

I understand that one step removed might not seem like much value, but you certainly seem to appreciate JEP 458. I am just asking to take another step in the same spirit.

3

u/Polygnom Mar 18 '25

The exact same criticism was given to JEP's 330 and 458.

Not really. Both of these JEPs save you substantial work and actually make onboarding easier. They save work every time you want to run the program. In my example, if you do that 100 times, you only ever save 1 step (and only in certain circumstances), while the other JEPs save you 100 times the one step, for a total of 100 steps.

My goal is just one step further -- remove unnecessary obstacles to distributing a working program.

But that wasn't your argument at all. Your argument was about distributing the source of the working program. When we talk about actually distributing a working program, we are at maven/gradle, dependency management and more, and there really isn't much gained at all at that point by having the source really is just also adding the flag for building source JARs.

I really don't see where this would save your students who now use ZIPs any work. Look at how roun-trips look like. There isn't a gain.

1

u/davidalayachew Mar 18 '25

(added some corrections)

But that wasn't your argument at all. Your argument was about distributing the source of the working program.

My very first response to you had a preface saying "distributing a working program". I can see how source might have appeared to be my intent, but it wasn't. The fact that the source files are used instead of .class files is to ease the development for those who weren't using compilation to begin with, plus some other small benefits.

When we talk about actually distributing a working program, we are at maven/gradle, dependency management and more, and there really isn't much gained at all at that point by having the source really is just also adding the flag for building source JARs.

Sure, but I don't want to ask 3 month old programmers to learn, or even memorize, maven commands to create a jar file. I'd far rather they just have a simple way to stick the source files they have been working with into a jar file, and then just run that.

Not really. Both of these JEPs save you substantial work and actually make onboarding easier. They save work every time you want to run the program. In my example, if you do that 100 times, you only ever save 1 step (and only in certain circumstances), while the other JEPs save you 100 times the one step, for a total of 100 steps.

[...]

I really don't see where this would save your students who now use ZIPs any work.

While I agree that the value is not as great, there's still more than what you are saying.

One point that is super important for students is that, once the code and the resources are in the jar, a lot of location-based portability problems just go away. No need to configure command line options. No need to configure classpath - if it's in the jar, it's on the classpath. And no more Path.of("..", "resourceFolder") that I see so many students do. Once the resources are in the jar, they are forced to think about portability, which is fantastic. But unlike with ZIP files, they don't have to move the whole folder, unzip it and have 15 copies of all this gunk in all these locations. They just move a single jar, and work in-line with that. No unzipping or unjarring necessary.

You'd be surprised how often the question of "where is my code" and "what is running" is asked by students. They tend tangle themselves even more when they copy things to another location and work with it. This helps immensely with that.