Kotlin Web-App Using Dropwizard


#1

I've just recently discovered Kotlin and decided to recreate a small web-app I developed using pure Java with Dropwizard 0.6.2.

This new project has such a structure:

package com.christian.service

public class MyService: Service() {
  public override fun initialize(bootstrap: Bootstrap?) {
  bootstrap?.setName(“kotlin-service”)
  }

  public override fun run(configuration: MyConfiguration?, environment: Environment?) {

  }
}

And the corresponding main class:

package com.christian.service;

public class Main {
  public static void main(String[] args) throws Exception {
  new com.christian.service.MyService().run(args);
  }
}

Dropwizard, for those that haven't used it, comes with a built-in Jetty Server that you start up with the following command:

java -jar target/KotlinTest.jar server kotlin-configuration.yml

However, I get the following ClassDefNotFoundException:

Exception in thread "main" java.lang.NoClassDefFoundError: jet/JetObject
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at com.syncofy.service.Main.main(Main.java:12)
Caused by: java.lang.ClassNotFoundException: jet.JetObject
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 13 more

I've already tried using

java -cp kotlin-runtime.jar; com/christian/service server kotlin-configuration

but that doesn't work:

Error: Could not find or load main class com.christian.service

Any suggestions?


#2

Hi Christian,

You are correct in that you’ll need to include the kotlin runtime.

However, your second attempt needs quotes and the full name of the class. Something like this should work:

java -cp “<full-path-to>/kotlin-runtime.jar;target/KotlinTest.jar” com.christian.service.Main

Cheers,
Andrew


#3

Thank you very much for your help! It was indeed the missing quotes, the full command is then:

java -cp "kotlin-runtime.jar;target/KotlinTest.jar" com.christian.service.Main server kotlin-configuration

This worked like a charm and brought my server up and running. Kotlin is very impressive!