I need to create a runnable Jar file which is a console app from the Kotlin project. I set up this project using gradle init. I tried all solutions I could find online by moving the main method out of the class, wrapping it in a companion object, and using JvmStatic annotation but I still keep getting an error message "Error: Main method is not static in class com.yk.ConsoleGradle.App, please define the main method as: public static void main(String[] args)"
I am using ./gradlew jar to build the project
class App {
val greeting: String
get() {
return "Hello World!"
}
fun main(args: Array<String>){
println("Hello kotlin")
}
}
application {
// Define the main class for the application.
mainClass.set("com.yk.ConsoleGradle.AppKt")
}
tasks.jar{
manifest {
attributes(mapOf("Implementation-Title" to project.name,
"Implementation-Version" to project.version, "Main-Class" to "com.yk.ConsoleGradle.App" ))
}
}
Here is the output of jar tf
jar tf /ConsoleGradle/app/build/libs/app-1.0.1.jar
META-INF/
META-INF/MANIFEST.MF
META-INF/app.kotlin_module
com/
com/yk/
com/yk/ConsoleGradle/
com/yk/ConsoleGradle/AppKt.class
com/yk/ConsoleGradle/App.class
Any help appreciated.
If you don't want to declare the main function inside the App class you could just create an App.kt file with the following code:
class App {
val greeting: String
get() {
return "Hello World!"
}
}
fun main(args: Array<String>){
println("Hello kotlin")
}
also you need to update your gradle jar task as:
jar {
manifest {
attributes "Implementation-Title": project.name
attributes "Implementation-Version": project.version
attributes "Main-Class": "com.yk.ConsoleGradle.AppKt"
}
}
notice the Main-Class is specified as AppKt instead of App.
This is because the main function is a top-level function and for top-level functions inside App.kt the Kotlin compiler creates a class called AppKt.class.