Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 647
Sam Berry Avatar asked Aug 31 '18 22:08

Sam Berry


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)
        }
    }
}
like image 66
Sam Berry Avatar answered Nov 16 '22 16:11

Sam Berry


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"
}
like image 33
LiorH Avatar answered Nov 16 '22 14:11

LiorH


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!")
        }
    }
}
like image 1
Artemiy Avatar answered Nov 16 '22 16:11

Artemiy