📜 ⬆️ ⬇️

Reducing the size of the distribution in Java 9

Four months later, it is planned to release Java 9 (we hope that the postponement will not happen anymore). Nothing prevents us from checking out on the preliminary version how much the main feature of the new version - modularity ( Jigsaw project) - meets the expectations.



This article describes the changes required to build a distribution kit with modularity support. The distribution kit is built on Windows , macOS and Linux operating systems.

The complete list of new features for JDK 9 is impressive . The Jigsaw project is the main goal of version 9. The subject of this article is an integral part of the Jigsaw project - JEP 275: Modular Java Application Packaging and related:
')

The JDK includes the Java Packager command line utility that allows you to compile, build, create digital signatures, and distribute Java applications. The functions that the Java Packager performs, starting with JDK 7 Update 6 , are also available as tasks for Ant . In the official documentation, they are called JavaFX Ant Tasks , because previously intended primarily for JavaFX applications.

JEP 275: Modular Java Application Packaging aims to integrate modularity into the Java Packager , including creating a JRE image that includes only the modules that the application uses. JavaFX Ant Tasks can also be called from Maven , the dominant build tool for today, through the Maven AntRun Plugin .

In the experiments were used:


Sample code is available on GitHub . Maven is used to compile and build the distribution. The project consists of four parts (modules in Maven terminology):


Distributions are built in all three operating systems.

Build distribution without the use of modules


The process of building the distribution consists of several steps:


To create distributions in macOS and Linux, nothing is needed additionally (except for JDK and Maven ). In the Windows operating system , you must first install Inno Setup or WiX Toolset . The following assumes Inno Setup is used .

Creating a JAR file with Maven is easier to do in the usual way, i.e. using the Apache Maven JAR Plugin (another way is to use <fx: jar> ). In the manifest ( MANIFEST.MF file) of the JAR file used to build the distribution package of a non-modular (without using Java 9 modules) application, the Main-Class must be specified. For this reason, the use of the above plug-in should be explicitly specified with the indication in the configuration of the main class.

Description of the use of the <fx: deploy> task in the pom.xml file:


Running compilation and build distribution in Windows and macOS :

 mvn clean package -P native-deploy 

Launching the compilation and build of the distribution in Linux (in addition, a tar.gz archive file is created):

 mvn clean package -P native-deploy,tar-gz 

Files of the created distribution kit with the exe extension, dmg are located in the <module name>/target/deploy/native directory, with the tar.gz extension are in the <module name>/target directory.

Add module usage


At the stage of the formation of the JRE image, jlink will implicitly be involved, to which it is necessary to transmit information about the modules used.

To determine which Java modules the application uses, the jdeps utility must be manually started. In version 9, it additionally shows the modules used.

 jdeps -s <name>.jar 

The first application ( console ) uses a single java.base module, the second ( desktop ) - java.base and java.desktop modules (together with transitive modules, since java.desktop itself depends on other modules).

For the first application, you need to add the module-info.java file containing (an indication of the basic java.base module is optional):

 module console.modular { } 

The module-info.java file for the second application should contain:

 module desktop.modular { requires java.desktop; } 

In the pom.xml file, the required parameters for the path to the module files (the path to the standard $JAVA_HOME/jmods modules are optional) and the module names of the application appear. It is unnecessary to include an application JAR as a resource, since it is already included as a module in the JRE image.



Running the compilation and build is similar to the previously specified.

Comparison of results


Size in megabytes of the JRE image included in the distribution kit and in the distribution file itself, created using the Java Packager :
applicationWindowsmacOSLinux
JRE imagedistribution kit (exe)JRE imagedistribution (dmg)JRE imagedistribution (tar.gz)
console-nonmodular165361816920472
desktop-nonmodular165361816920472
console-modular359351344sixteen
desktop-modular721875298531

Size in megabytes of the JRE image created by manually launching jlink with the most optimal set of parameters (not available from the Java Packager ):

 jlink --module-path <module path> --add-modules <modules> --output <directory> --compress=2 --no-header-files --no-man-pages --strip-debug 

applicationWindows JRE imagemacOS, JRE imageLinux JRE image
console-modular222129
desktop-modular383949

Most of all, the size of the JRE image has decreased on macOS :


findings


A significant reduction in the size of the distribution is achieved relatively easily for simple projects. In the case of more complex projects, at least on the preliminary version of JDK 9 , it may be necessary to study the Java Packager code to solve problems.

At the conferences on April 4 ( JBreak 2017 in Novosibirsk) and April 7-8 ( JPoint 2017 in Moscow), you can listen to reports on other interesting features of Java 9 and the following Java versions:


UPD: The topic continues in the article .

Source: https://habr.com/ru/post/324318/


All Articles