Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Java Platform Module System reduce a JAR size?

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?

like image 376
wcobalt Avatar asked Jul 03 '19 16:07

wcobalt


People also ask

What does Java module do?

A Java module is a set of packages that declares which of them form an API accessible to other modules and which are internal and encapsulated — similar to how a class defines the visibility of its members. A module also declares what other modules it requires for its operation.

What are the benefits of Java modules?

Advantages of Java SE 9 Module System As Java SE 9 is going to divide JDK, JRE, JARs etc, into smaller modules, we can use whatever modules we want. So it is very easy to scale down the Java Application to Small devices. Ease of Testing and Maintainability. Supports better Performance.

Which of the following is correct about module system in Java 9?

Q 7 - Which of the following is correct about Module System in Java 9? A - javac, jlink, and java have additional options to specify module paths, which further locate definitions of modules.

Is a jar file a module?

A modular JAR file is a JAR file that has a module descriptor, module-info. class , in the top-level directory (or root) directory. The module descriptor is the binary form of a module declaration.


1 Answers

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.

like image 197
Marko Previsic Avatar answered Oct 07 '22 09:10

Marko Previsic