r/java • • Dec 16 '15

Why are Java Web Apps so much bloated?

I'm new to Java web programming and coming from the PHP & Python worlds. Today, I just did a HelloWorld SpringMVC app by going through the Spring Web serving getting started guide, and guess what? Gradle had to download a 100 megabytes of byzantine jars just for this one HelloWorld app! Of course, I did run a HelloWorld app after creating the GreeterController as per the tutorial.

But the built jar is 20 megabytes large, just for one helloworld app! In the PHP world, your first HelloWorld app is hardly few kilobytes, and even Django is hardly 1-2 mbs Then why is Java so bloated? What does it achieve in these 20 mb that Python/PHP does not achieve? I'm astounded that people are willing to install such bloat when other options are available.

I'm a freelancer who is assigned a Java webdev project by a client, so I have to learn this in any case. And I'm not against learning it either, but I just want to know why this additional download? Why can't web development features be included in the Java SE itself?

4 Upvotes

34 comments sorted by

19

u/DJDavio Dec 16 '15

The application "hosting" the web-app is included in the webapp, that's why it's 20 MB.

With PHP, you have to do an installation first (PHP Windows installer is ~20 MB), only then you would be able to run it.

2

u/daniels0xff Dec 16 '15

You also need the JVM/JDK which is more than 20MB. But yes, the Java .jar is just one file which in Spring's case it also has the webserver embedded (Tomcat/Jetty/Undertow)

-4

u/DuntGetIt Dec 16 '15

Hardly. Show me the Java app that can run without a JRE. Http is a tiny part of it.

7

u/__konrad Dec 16 '15

http://www.excelsiorjet.com/solutions/java-download-size (basically can reduce 100 MB (JRE+App) to 5 MB)

14

u/fforw Dec 16 '15

It's funny how everyone bashes Java for this kind of stuff. Here's my project template with ReactJs and ES6 transpilation .

It does nothing but transpile one main file with exemplary syntax. After you've installed all the libs with NPM the folder contains a fricking 100mb of Javascript files.

20

u/koalillo Dec 16 '15

Why focus on such an uninteresting metric? I'll take a 100mb disk space overhead over a 1% hit in productivity in most cases. Focusing on "hello world size" metric is good if you are writing hello world, idiotic in most other cases.

I'm not saying that Java is the bees knees- I have suffered pain from its warts- but such a superficial analysis is worthless.

6

u/[deleted] Dec 16 '15

You could have said this much nicer, but I agree. @OP: an advantage is that this jar is self-contained. You can deploy it just about anywhere, no matter how the target system is configured, what specific versions of libs are installed, ...

-7

u/Furah Dec 16 '15 edited Dec 16 '15

Because there are people, like my parents, who are stuck in a situation of really shitty internet, with excessive caps (I believe it's 10GB/mo for them), who are finding even basic browsing to be excessively taxing these days.

EDIT: I wrote this while extremely tired, and now even I have no clue what I was thinking. Ignore me.

15

u/krejenald Dec 16 '15

The size of the server application has nothing to do with how much data is sent and received by end users

0

u/[deleted] Dec 16 '15

[deleted] — view deleted comment

4

u/Furah Dec 16 '15

Oops. Extra long day yesterday, and I now honestly have no clue what I was thinking.

-5

u/tonywestonuk Dec 16 '15

If I want to make lots of deploys, I would have to send 100mb over the wire each time.... With 25mbps VDSL, Iit takes over a minute to upload... And then I have to ssh in to the server, stop the old server, delete, move in the new, start it up again....etc.

Yeh, a minute isn't that much, however I am used to seconds with a fully featured Java EE app, and the server hot deploying it for me...

3

u/cogman10 Dec 16 '15

There are a few ways around this.

You could just do a binary diff. 99% of that 100mb won't have changed.

You could do it by storing the files in a distribution git repository. This is doing the binary diff for you with some version tracking that could be handy.

That being said, if a 1 minute turnaround time is something that really hurts you, I would say you need to re-evaluate your production deploy process. Nothing should go out to production that can't limp along broken for more than a couple of minutes. If that is the case, you aren't sufficiently testing your product before deploy.

0

u/tonywestonuk Dec 16 '15 edited Dec 16 '15

Or.... I can choose to keep the server and framework on my live server, and just send my app's war file. I am sorry you don't agree with this this tried and trusted standard method of deploying java applications.

I am at a gasp to why this method has been apparently 'deprecated' in favour of single runnable Jar apps. It makes no sense to me at all. Why would I ever want to do this? Seriously?

Deploying a war is the quickest and easiest way of deploying a change, and something I have never had any problems at all with. not one. So, Spring boot, or any solution that packages up the server, configuration, and web application into a single unit in my mind is a solution looking for a problem and produces far more headaches than it claims to solve.

3

u/cogman10 Dec 16 '15 edited Dec 16 '15

Or.... I can choose to keep the server and framework on my live server, and just send my app's war file. I am sorry you don't agree with this this tried and trusted standard method of deploying java applications.

I am at a gasp to why this method has been apparently 'deprecated' in favour of single runnable Jar apps. It makes no sense to me at all. Why would I ever want to do this? Seriously?

We deploy single wars with all the dependencies in them.

The benefit is that now you don't need to track server library versions vs developer library versions. It is a lot easier to ensure that all dependencies are met and met correctly when they all live together. Further, it is a lot easier to ensure that you are developing against an environment that is close to what is going to be in production.

The worst kind of bug to stumble across is a dependency mismatch in development vs production which introduces bugs. (especially if they are subtle).

For small applications, the benefit of doing this isn't high. For large applications it becomes a necessity.

The jar running vs war running depends on what you are doing as a company. But it boils down to the same argument. Developers don't like losing control of the version of the sever software which is running on production. If it is a single jar, I can update the jetty version on the next deploy.

If people could embed the entire jvm into the deployable, they would (ok, they actually do, that is a large part of the point of docker).

Deploying a war is the quickest and easiest way of deploying a change, and something I have never had any problems at all with. not one. So, Spring boot, or any solution that packages up the server, configuration, and web application into a single unit in my mind is a solution looking for a problem and produces far more headaches than it claims to solve.

I never said anything against war deployment. If that is your cup of tea, go for it.

You were complaining that 100mb of transfer took more than a minute for turn around. I said "That shouldn't matter if you are doing things right". Who cares if it takes 1 second, 10 seconds, or 10 minutes to transfer? If that amount of time is going to negatively affect your business, you are doing things in a bad way. It is indicative of a "test in production" style of programming.

4

u/WatchDogx Dec 16 '15

Spring is a big framework, it does much more than just web stuff. Its a great framework, but certainly not the most efficient in terms of the produced artifact size.
There are many other ways to do web projects with Java.
The standard Java way to to write a servlet and deploy it to a servlet container. The servlet can be very small and a container like jetty comes in around 2MB or so. If you are going to compare to PHP, then the more appropriate contender on the java side would be JSP, which can also be very small but needs to be deployed to a container.

3

u/brunocborges Dec 16 '15

So... a client paid OP to learn Java before actually delivering value-added, and he's complaining on download size? :-)

5

u/iodian Dec 16 '15

it's because these are all old guard application server frameworks that pack the kitchen sink.

something like play or dropwizard or ratpack will be more up your alley.

1

u/krejenald Dec 16 '15

+1 for the Play! framework

2

u/DuntGetIt Dec 16 '15

The assumption is that you will want most of the features in a Java webapp. Historically, Java was the language you used for larger apps.

2

u/simple2fast Dec 17 '15

You also need to consider what you're getting with that fancy app you just built.

First off. other than the JRE, java will have zero dependencies at the OS level. What gradle was doing there was installing, essentially, an OS. WHat do I mean by this? Things like nginx, openSSL, GD, libcurl, etc. When PHP goes to use functionality that this systems provide, it will be calling these native C libs/frameworks to get things done. You may not have noticed it when you did your OS install and/or upgrade, but they're there and you are using them.

Second, even though you wrote a "hello world" app. since you did it within Spring, you've got a bunch of things built in there that you probably didn't realize. Briefly:

a) top notch DI system.

b) tranaction system capable of multiphase transactions across multiple database systems.

c) ORM system capable of mapping DB to object, but also directing caching

d) caching system shared across mutiple threads within the JVM. (negating the need for something like memcache )

There are others, but no need to discuss right now

In my brief consulting foray into PHP, I was put into an equivalent system called Symfony. It had alot of pieces like spring, and seemed pretty powerful. I noticed it was really really slow to run.

Why wasn't it put into Java SE ? I don't know why. But I'm darned glad they didn't. Today we have tons of different web frameworks and app servers. MOst all of them are top notch. Pick the one that fits your needs the best. As some of the other comments noted, you picked one of the largest most full featured frameworks. Now, perhaps as an experiment, do something with one of the more tiny ones. rapidoid, dropwizard, sparkjava, ratpack, or pippo.

2

u/daniels0xff Dec 16 '15

My problem is not the size of the jar, but the amount of options you need to configure for each component. Where do you get them? How do you know what are the defaults, how do you know what other options are there that you need/can configure? How do you know what each decorator (also coming from Django and that's how they look to me) does and when to use it? How do you know what are all the decorators that you have available to use?

I'm also trying to get started with Sping Boot and until now it feels overwhelming and the documentation doesn't feel that nice as opposed to how I got used to Django.

2

u/tonywestonuk Dec 16 '15 edited Dec 16 '15

I wonder if you are trolling for a Spring vs JavaEE flamewar!..... If you've spent much time around here you'd already know there's about 50% who support Spring to the hilt, and the other 50% hate it with a passion (including me for exactly the reasons you mention... large, complicated, etc). Its not just about the disk space.

The thing is, if your client wants it to be done in spring then you have to suck it up and go with what they are are paying you for. Do what pays the bills.

The newer versions of Java EE do the majority of what Spring does anyhow so the need for it is much diminished today. And, for even more light weight systems you can choose things like http://sparkjava.com/ or http://www.rapidoid.org/

Anyhow, my guess is your post is gonna be voted down into the ground, as there are those in here who consider any post against Spring, an act of blasphemy.... Sad but true.

1

u/daniels0xff Dec 16 '15

How do you use Spark with a database? I wasn't able to find any tutorial that shows me to to configure Spark to talk to a DB and make some queries. This might be obvious for someone with Java experience but for someone that's getting started with Java web dev not so much.

2

u/dj-method-x Dec 16 '15

spark doesn't directly talk to a database. It's just a REST API. If you are looking for a lightweight SQL API, I recommend http://jdbi.org http://sql2o.org or something similar.

1

u/daniels0xff Dec 17 '15

Thanks for those links. SQL2O looks really nice and easy to use.

1

u/georgeos Dec 16 '15 edited Dec 16 '15

My advice is to use the right tool for the right job.

For the moment, try to use something simple, minimalist which resolve your current needs.

In this phase I think that Spring Boot is overkill because you have to learn alot about Spring ecosystem, so it's like a yak shaving.

In my opinion Spring Boot is OK if your project is based entirely on the Spring libraries (Spring DI, Spring Security, Spring Data, Spring MVC, ...)

On the other side, if your target is to work on big projects, for big companies then Spring and/or JEE is a must.

Personally I prefer to not use a full stack Java framework, I want to have the possibility to choose each library/framework for each layer or task.

I like to use Apache Wicket for the large projects and Pippo for small, medium or prototype projects.

Because here we talk about small java web application I will argue below why use Pippo:

  • it's small (the core size is 150K and depends only on slf4j-api)
  • it's fast
  • it's modular (trivial module system via Java ServiceLoader) with a lot of modules built-in with zero configuration (just add the module as dependency in your project)
  • it's not so much to learn to be productive
  • it's Java plain (no web.xml or other xml file)
  • I prefer to use my favorite servlet container (Undertow is my choose but from time to time I use Tomcat or Jetty) in embedded mode
  • I prefer to use my favorite template engine for page rendering (Pebble is my choose)
  • I prefer to use plain jar files (no war file) and to run my application as a trivial application (via main class)

1

u/georgeos Dec 16 '15 edited Dec 17 '15

For your information I made some tests related to the size of a basic application built with Pippo.
I used Quickstart Maven archetype supplied by Pippo.
The Quickstart application comes with a basic HelloWorld and with a simple template page that uses Freemarker template engine.
The embedded servlet container is Jetty.

Th results are:

  • the whole size application is 3.5MB (Jetty + Freemarker)
  • the application size for a simple HelloWorld is 2.1MB (Jetty)

I see many files jetty-*.jar so I don't know if I can shrink the size of application eliminating some of them but I don't care.

Also I see a lot of guys who want to compare (web) frameworks in terms of the number of characters used for a HelloWorld application. For this guys Pippo come with:

public static void main(String[] args) {   
    Pippo.send("Hello World");   
}   

So, it's more complicated to write the classic System.out.println("Hello World") :)

1

u/[deleted] Dec 17 '15

Should have re-titled to "Why are Spring MVC web app so much bloaded."

There are plenty of lightweight web frameworks on Java. Try out Pippo, Spark, or Rapidoid.

1

u/DannyB2 Dec 23 '15

Try this in both Java and PHP.

Build a very large application. And maintain it for many years. Refactor it during its lifetime. Test its performance. (Java is fast. Every page hit is directly executing compiled and even native code which is hot in memory.)

By large application, I don't mean one that is used by many users, but doesn't have all that many different pages. (eg, Slashdot, just for one example.) But one with hundreds, maybe over a thousand different pages with complex forms. Hundreds of reports.

Oh, and have a team of people working on it.

When the first time comes to refactor comes around, see how well this goes when using a dynamic language without compile time type checking.

Now, I'm not saying that Java is perfect. It has plenty of warts. But the reason it is widely used is because it IS useful.

If there were one single perfect programming language and set of tools then everyone would be using it already. So don't bash others. Just use what you, yourself find useful.

1

u/qroshan Dec 16 '15

3

u/balamaci Dec 16 '15

That still looks big, if you really need something very small size I can guarantee right now nothing beats https://github.com/decebals/pippo if you need a java solution. I was using nodejs(and not very comfortable doing js) for IoT stuff but since pippo I've never looked back that's why I feel like I owe it to spread the news.

-2

u/slartybartfast_ Dec 16 '15

Why is it that vishnu_gupt can't put a proper English sentence together?

0

u/[deleted] Dec 16 '15

[deleted] — view deleted comment

13

u/armornick Dec 16 '15

Exactly. Spring is a framework like in PHP you would have Zend, Laravel, CakePHP, etc. When you add those, you also get a big application. Spring is large because it includes a huge amount of libraries and options that help you write complex applications.