gradlejettyktor

Missing Jetty HttpConnection class in Ktor fat JAR


I'm trying to build my Ktor application using the official plugin but once I run the built fat JAR, I get the following error when requesting my app:

$ ./gradlew runFatJar

...

INFO  Application - Responding at http://0.0.0.0:8080
WARN  o.eclipse.jetty.io.ManagedSelector - java.lang.ExceptionInInitializerError
WARN  o.eclipse.jetty.io.ManagedSelector - java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.jetty.server.HttpConnection

The fat JAR seems to be missing a Jetty class.


Solution

  • The Ktor plugin is based on Shadow, you have to call mergeServiceFiles() to ensure all the required classes are added to the JAR:

    tasks {
        named<ShadowJar>("shadowJar") {
            mergeServiceFiles()
        }
    }