Dropwizard Kotlin "Main method is not static in class" when running java -jar

Viewed 4624

I am attempting to start my Dropwizard Kotlin application. When running:

java -jar target/application-1.0.jar server environment.yml

I get the following error:

Error: Main method is not static in class, please define the main method as: 
   public static void main(String[] args)
3 Answers

Make sure that the main method in your application class is defined inside of a companion object with @JvmStatic:

class MyClass {    
companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            Application().run(*args)
        }
    }
}

function main can be a top-level function (let's say in a file called foo.bar.MainApp.kt):

fun main(args: Array<String>) {
   Application().run(*args)
}

If building and packaging with gradle and the application plugin then configure that with:

application {
    mainClassName = "foo.bar.MainAppKt"
}

In build.gradle:

plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.41'
id 'application'
}
mainClassName = 'Main'

In Main.kt:

class Main {

    companion object {

        @JvmStatic
        fun main(args: Array<String>) {
            println("Hello World!")
        }
    }
}
Related