The correct way to start a kotlin application from a gradle task

I have a simple script

package com.lapots.game.journey.ims.example fun main(args: Array<String>) { println("Hello, world!") } 

And here is the gradle task

 task runExample(type: JavaExec) { main ='com.lapots.game.journey.ims.example.Example' classpath = sourceSets.main.runtimeClasspath } 

But when I try to run the gradle runExample task, I get an error

Error: Could not find or load main class com.lapots.game.journey.ims.example.Example

What is the correct way to run the application?

+6
source share
2 answers

Due to the link how to execute the compiled class file in Kotlin? provided by @JaysonMinard that main

 @file:JvmName("Example") package com.lapots.game.journey.ims.example fun main(args: Array<String>) { print("executable!") } 

and what task

 task runExample(type: JavaExec) { main = 'com.lapots.game.journey.ims.example.Example' classpath = sourceSets.main.runtimeClasspath } 

did the trick

+4
source

You can also use the gradle application plugin for this.

 // example.kt package com.lapots.game.journey.ims.example fun main(args: Array<String>) { print("executable!") } 

add this to your build.gradle

 // build.gradle apply plugin "application" mainClassName = 'com.lapots.game.journey.ims.example.ExampleKt' 

Then run the application as follows.

 ./gradlew run 
0
source

All Articles