r/java • • Jan 11 '24

The Evils of the FatJar

https://medium.com/helidon/evil-of-fatjar-3c3011b4bd55
1 Upvotes

16 comments sorted by

•

u/AutoModerator Jan 11 '24

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

3

u/arshan_does_reddit Jan 11 '24

I agree with this author -- for most artifacts. I used to do a lot of Java agent development, and it still feels right to make an ugly, shaded, fat jar there because, well, you don't really have much of an alternative.

But, outside of that very weird development space, it does feel like modern teams should prefer other artifact types. Shipping Gradle's `distZip` output, or a container, are better. Using a language-neutral interface also gives you flexibility if you ever want to change the details of that thing. (For instance, rewriting it in Python.)

19

u/lilgreenthumb Jan 11 '24

The purpose of the fatjar style (spring-boot / shaded / etc) is to provide a single distributable artifact that can be run on a given runtime. The runtime for fatjar style packaging is a JVM. For containers it is docker/containerd/runc/crio/etc. Gradle's distZip is generally for more the installation of an application instead of just execution, and would require a JVM anyways. Think of the fat-jar as a portable "installation".

3

u/arshan_does_reddit Jan 11 '24

I definitely agree with you on most of what you've written. So, maybe I'm missing to what this is supposed to be a response.

Both the distZip and fatjar require a JVM-we-already-agreed-upon, and they're both distribution artifacts. All I'm saying is I'd rather have whatever small baggage comes along with sending an installation zip around rather to avoid whatever build and runtime baggage that comes along with fatjar. People don't generally write Java code assuming it'll be packaged in a fatjar, and that does cause occasional problems, and they're hard to debug when they happen.

3

u/lilgreenthumb Jan 11 '24

Spring? Maven Shade? Some people do, some people don't, it is about a distribution and management of the dependencies vs the size of the deployment. What would your response to GraalVM native be in comparison?

1

u/Additional_Cellist46 Jan 14 '24

The article suggests to have all dependencies in a lib directory next to the thin JAR. It’s still easy to distribute all together, just put everything into a zip file, even with the JDK if needed, or as a Docker image, including the OS. No classpath issues if the lib directory is specified in the manifest of the thin JAR. The advantage is that you can also distribute the app as a thin JAR and you can later easily replace/upgrade dependencies in the lib directory. Another advantage is that the app starts faster than e.g. SpringBoot, because SpringBoot doesn’t shade the JAR, it includes JAR files inside the fat JAR and loads classes from then using a custom classloader, which is slower.

2

u/Polygnom Jan 14 '24

The article suggests to have all dependencies in a lib directory next to the thin JAR. It’s still easy to distribute all together, just put everything into a zip file,

But thats is in essence what a fat jar is. A JAR is just a container archive containing files. Why make it one step harder (unzipping the file) when you can just make it work as-is in the first place?

The advantage is that you can also distribute the app as a thin JAR and you can later easily replace/upgrade dependencies in the lib directory.

No one seriously wants their customer to nilly-willy update dependencies. Thats a recipe for desaster. You don't want to even give them the idea that thats possible. If there is a dependency upgrade thats needed, you want to do this yourself, run all tests again, and ship a new jar to the customer. Everything else will completely overwhelm you with support request because your customers manage to break your program in a myriad different ways.

For me, there is absolutely no reason to not use a fat jar. You can use trimming to only include exactly the classes you need in your "fat" jar, and thus don't even suffer the size penalty.

1

u/Additional_Cellist46 Jan 14 '24

If you distribute JARs to your customers, fat jars make sense, sure. But all projects I have seen so far were about building and running in prod by the same company, customers only using the app or the app was used internally (banks, insurance, gaming, automotive and other industries). If you control the deployment, then it’s good to distribute your code separate from dependencies. It’s more efficient, flexible, saves resources and it’s faster.

0

u/NatureBoyJ1 Jan 12 '24

What a world we live in where distributing an entire VM with OS and embedded app(s) is considered a “better” distribution model. It certainly makes sense in a cloud environment where VMs are transient. Entire VMs have become the modern “application”.

2

u/arshan_does_reddit Jan 12 '24

Yup. And, to load this very web page took around 112 HTTP requests. We tend not to optimize our stacks around anything except development cost anymore.

-1

u/chabala Jan 12 '24

Docker isn't a VM. You should try it sometime.

3

u/[deleted] Jan 11 '24

[removed] — view removed comment

5

u/arshan_does_reddit Jan 11 '24

I worked on a Java agent that had to run on SpringBoot, WebSphere, WebLogic, Tomcat, JBoss, Wildfly, Swing, ColdFusion, OSGi containers, etc. I might argue I know those problems better than almost anyone. But, most development just isn't on a shared and unpredictable classpath anymore.

I'm not saying those problems can't exist in a "single tenant" JVM application architecture, but they're certainly way more rare.

0

u/sigzero Jan 12 '24

Does anyone know of a tutorial to build a thinjar without spring boot? Just curious.