Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Last active May 23, 2020 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasdarimont/c005dffe4ed5ed056915d9c418587930 to your computer and use it in GitHub Desktop.
Save thomasdarimont/c005dffe4ed5ed056915d9c418587930 to your computer and use it in GitHub Desktop.
Create a trimmed down JDK with support for remote debugging, full jmx support that can run Wildfly / Keycloak
JAVA_TARGET=$PWD/java-runtime

echo "Create trimmed down JDK" && \
      $JAVA_HOME/bin/jlink \
      --no-header-files \
      --no-man-pages \
      --compress=2 \
      --vm=server \
      --exclude-files="**/bin/rmiregistry,**/bin/jrunscript,**/bin/rmid" \
      --module-path "$JAVA_HOME/jmods" \
      --add-modules java.base,java.instrument,java.logging,java.management,java.se,java.naming,java.security.jgss,java.security.sasl,java.sql,java.transaction.xa,java.xml,java.xml.crypto,jdk.security.auth,jdk.xml.dom,jdk.naming.dns,jdk.unsupported,jdk.crypto.cryptoki,jdk.crypto.ec,jdk.jcmd,jdk.internal.ed,jdk.internal.jvmstat,jdk.internal.le,jdk.internal.opt,jdk.internal.vm.ci,jdk.internal.vm.compiler,jdk.internal.vm.compiler.management \
      --output $JAVA_TARGET && \
      echo "Copy Java libraries from JDK" && \
      cp $JAVA_HOME/bin/jstack $JAVA_TARGET/bin/ && \
      cp $JAVA_HOME/bin/jinfo $JAVA_TARGET/bin/ && \
      cp $JAVA_HOME/bin/jstat $JAVA_TARGET/bin/ && \
      cp $JAVA_HOME/bin/jcmd $JAVA_TARGET/bin/ && \
      echo "Add Java Debugging tools from JDK" && \
      cp $JAVA_HOME/lib/libjdwp.so $JAVA_TARGET/lib/ && \
      cp $JAVA_HOME/lib/libdt_socket.so $JAVA_TARGET/lib/ && \
      cp $JAVA_HOME/lib/*management*.so $JAVA_TARGET/lib/ && \
      echo "Finished creating trimmed down JDK."

This produces a 71MB JDK

$ du -sh java-runtime 
71M	java-runtime

Remove debug symbols from jvm.so

strip -p --strip-unneeded $JAVA_TARGET/lib/server/libjvm.so

This yields:

$ du -sh java-runtime 
67M	java-runtime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment