Reducing JRE size

Until Java 9 is ready, there is still an option if you need to reduce the size of a bundled JRE: exclude some files!

This is totally official and supported by Oracle; in fact all the files you can exclude from a JRE are listed in the JRE’s README (unfortunately with a few persisting typos…) . All you have to do is adapt your build process; for example if you’re including the JRE as a resource in a Maven pom, you could do this:

           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>  <!-- fecth and unzip JRE -->
                    <execution>
                        <id>fetch-jre</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.oracle</groupId>
                                    <artifactId>jre</artifactId>
                                    <version>1.8.121</version>
                                    <classifier>windows-x64</classifier>
                                    <type>zip</type>
                                    <overWrite>true</overWrite>
                                    <!-- Exclude optional files as per http://www.oracle.com/technetwork/java/javase/jre-8-readme-2095710.html
                                        to decrease JRE size -->
                                    <excludes>jre/lib/ext/*,jre/bin/rmid.exe,jre/bin/rmiregistry.exe,
                                        jre/bin/tnameserv.exe,jre/bin/keytool.exe,jre/bin/kinit.exe,jre/bin/klist.exe,
                                        jre/bin/ktab.exe,jre/bin/policytool.exe,jre/bin/orbd.exe,jre/bin/servertool.exe,
                                        jre/bin/javaws.exe,jre/lib/javaws.jar,jre/bin/jfr.exe,jre/lib/jfr.jar,
                                        jre/bin/javaw.exe,jre/bin/javacpl.exe,jre/bin/jucheck.exe,
                                        jre/bin/dtplugin,jre/bin/jabswitch.exe,jre/bin/java_crw_demo.dll,
                                        jre/bin/JavaAccessBridge*.dll,jre/bin/JavaAccessBridge.dll,
                                        jre/bin/JAWTAccessBridge*.dll,jre/bin/JAWTAccessBridge.dll,
                                        jre/bin/WindowsAccessBridge*.dll,jre/bin/WindowsAccessBridge.dll,
                                        jre/bin/wsdetect.dll,jre/bin/dtplugin/*,jre/bin/plugin2/*,jre/bin/deploy.dll,
                                        jre/bin/jfr.dll,jre/bin/javacpl.cpl,jre/lib/deploy.jar,jre/lib/plugin.jar,
                                        jre/lib/deploy/*,jre/THIRDPARTYLICENSEREADME-JAVAFX.txt,jre/lib/ant-javafx.jar,
                                        jre/lib/javafx.properties,jre/lib/jfxswt.jar,jre/bin/decora-sse.dll,
                                        jre/bin/fxplugins.dll,jre/bin/glass.dll,jre/bin/glib-lite.dll,
                                        jre/bin/gstreamer-lite.dll,jre/bin/javafx_font.dll,jre/bin/javafx_font_t2k.dll,
                                        jre/bin/javafx_iio.dll,jre/bin/jfxmedia.dll,jre/bin/jfxwebkit.dll,
                                        jre/bin/prism_common.dll,jre/bin/prism_d3d.dll,jre/bin/prism_es2.dll,
                                        jre/bin/prism_sw.dll</excludes>
                                    <outputDirectory>src/main/resources</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Note: this artifact is not available from Maven Central but from the repository at http://nexus.nuiton.org/nexus/content/repositories/jvm


Leave a Reply

Your email address will not be published. Required fields are marked *