Does the Java Platform Module System reduce a JAR size?
Asked Answered
B

1

7

Here the following benefit of Jigsaw is described:

As part of Project Jigsaw, all the Java Platform APIs have been split up into separate modules. The benefit of splitting all the Java APIs up into modules is that you can now specify what modules of the Java platform your application requires. Knowing what Java Platform modules your application requires, Java can package up your application including only the Java Platform modules that your application actually uses.

But, I can not understand how does it work, because as I know, Java does not put required modules directly in JAR. It just looks for them in module path. So, what the way does Java reduce JAR size by?

Bicyclic answered 3/7, 2019 at 16:1 Comment(4)
I think it doesn't reduce jars size, rather allows you to have a more minimal JVM, embedding only modules needed by your application. Jars have a well defined format already.Quevedo
You should probably read about jlinkBreather
where does it say that it "reduce JAR size"??Muldon
@CarlosHeuberger it doesn't. But I thought in that way, because it is said about packaging up only needed modulesBicyclic
B
7

Java can package up your application including only the Java Platform modules that your application actually uses.

Since Java 9, it is possible to create custom runtime images using the jlink tool.

Basically, a custom runtime image is a package that not only contains your 3rd party libraries but also required parts of the JVM. This is some kind of self-contained executable image that contains everything you need to run it, without the need to have a JRE installed on your OS. It's a nice feature because you don't have to worry if someone has a particular JRE version installed in order to run the image.

Now, I guess that the sentence you are referring to doesn't want to say that the size of the JAR is reduced when compared to a pre-Java-9 (non-modular) JAR. I think that the author wanted to say that the ability to package only the required modules of the JVM reduces its size in comparison with a JAR that would contain the whole JVM.

For example, imagine you are writing an application which makes use of only some basic Java API's like collections, IO or date & time API (contained in the java.base module). Since you aren't using API's like Swing (java.desktop module) or JDBC (java.sql module), you don't need to have them in your package. Therefore, you can explicitly specify which java modules your application is using (in the module-info.java file) and only those will be bundled with your app. In this case, you would only add the java.base module in the list of required modules. On the other hand, if the need arises to add database access to your application using JDBC, you will add the java.sql module to the list of modules. As you can see, there is no need to have a whole JVM bundled with your application since you can choose which modules of the JVM are needed.

Blackout answered 3/7, 2019 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.