B4J Tutorial [Server] Packaging OpenJDK for Server App

Discussion in 'B4J Tutorials' started by tchart, Apr 23, 2019.

  1. tchart

    tchart Active Member Licensed User

    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;

    java.io.IOException: Invalid keystore format
        at java.base/sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:
        at java.base/sun.security.util.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:
        at java.base/java.security.KeyStore.load(KeyStore.java:
        at org.eclipse.jetty.util.security.CertificateUtils.getKeyStore(CertificateUtils.java:
        at org.eclipse.jetty.util.ssl.SslContextFactory.loadKeyStore(SslContextFactory.java:
        at org.eclipse.jetty.util.ssl.SslContextFactory.load(SslContextFactory.java:
        at org.eclipse.jetty.util.ssl.SslContextFactory.doStart(SslContextFactory.java:
    Didier9, inakigarm, Chris2 and 4 others like this.
  2. Chris2

    Chris2 Member Licensed 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;
    <java 11>\bin\jdeps -s MyApp.jar
    This gives an output like;
    MyApp.jar -> java.base
    MyApp.jar -> java.logging
    Then, use jlink to create your own JRE including only the modules listed by jdeps;
    <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.
    inakigarm, ShaneG30 and alwaysbusy like this.
  3. tchart

    tchart Active Member Licensed User

    @Chris2 very nice find! Seems to work great (although for some reason jdeps doesnt find jfx)
  4. Chris2

    Chris2 Member Licensed User

    Thanks @tchart.

    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.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice