B4J Tutorial [Server] Packaging OpenJDK for Server App

I couldn't find this on the forum but if you are looking to package your server (non-UI) app with Java 11 (OpenJDK) then you should be aware there is no JRE per se.

I came across an article (link below) a few weeks back which helped me package a "JRE" for my server application;

From your JDK 11 distribution;
  1. Delete the jmods directory. This eliminates about 74 MB.
  2. Delete the lib/src.zip to save an additional 45+ MB.
The end result is a "...modified JDK 11 runtime consumes about 157 MB, nearly 40 MB smaller than typical Java 8 JREs".

Original article; https://dzone.com/articles/no-more-jre-packaging-no-big-deal-1

Important note; although I was able to start my app up with just the bin & lib folders it would fail when trying to bind the SSL certificate - the reason is that the security policies are under the "conf\security" directory. You need to include the conf folder. Just in case anyone runs into the same problem the error is below;

B4X:
java.io.IOException: Invalid keystore format
    at java.base/sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:659)
    at java.base/sun.security.util.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:222)
    at java.base/java.security.KeyStore.load(KeyStore.java:1479)
    at org.eclipse.jetty.util.security.CertificateUtils.getKeyStore(CertificateUtils.java:54)
    at org.eclipse.jetty.util.ssl.SslContextFactory.loadKeyStore(SslContextFactory.java:1071)
    at org.eclipse.jetty.util.ssl.SslContextFactory.load(SslContextFactory.java:262)
    at org.eclipse.jetty.util.ssl.SslContextFactory.doStart(SslContextFactory.java:229)
 

Chris2

Active Member
Licensed User
Longtime User
I may have found a possible improvement to this based on the subs in B4JPackager11 & the instructions at https://netnix.org/2018/07/19/windows-exe-bundled-with-openjdk/...

You can build your own OpenJDK11 JRE by using jdeps & jlink.
jdeps will list the java modules that your jar file uses;
B4X:
 <java 11>\bin\jdeps -s MyApp.jar

This gives an output like;
B4X:
MyApp.jar -> java.base
MyApp.jar -> java.logging

Then, use jlink to create your own JRE including only the modules listed by jdeps;
B4X:
 <java 11>\bin\jlink --module-path "<java 11>\jmods" --add-modules java.base, java.logging --output jre-11.0.1 --strip-debug --compress=2 --no-header-files
The 'personalised' JRE is written to the output folder (jre-11.0.1 in the example above).

Using this method for a small non-ui app that used java.base only, the JRE total size was <25MB.

I can't guarantee that this will work for everyone, but it did for me, and saved >100MB.
 

tchart

Well-Known Member
Licensed User
Longtime User
@Chris2 very nice find! Seems to work great (although for some reason jdeps doesnt find jfx)
 

Chris2

Active Member
Licensed User
Longtime User
Thanks @tchart.

although for some reason jdeps doesnt find jfx
I guess that's because jfx isn't included in OpenJDK11, it's a separate package now. So maybe the jdeps in OpenJDK doesn't know about jfx.

Which I guess means that the method in post #2 will only ever be suitable for non-ui apps.
 
Top