31

I've got simple script

package com.lapots.game.journey.ims.example


fun main(args: Array<String>) {
    println("Hello, world!")
}

And here is gradle task

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

But when I try to run task gradle runExample I get error

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

What is the proper way to run application?

lapots
  • 12,553
  • 32
  • 121
  • 242
  • 4
    Possible duplicate of [how to run compiled class file in Kotlin?](http://stackoverflow.com/questions/9355690/how-to-run-compiled-class-file-in-kotlin) – Jayson Minard Sep 19 '16 at 14:58
  • 1
    There is a section on using Gradle application plugin to run a Kotlin app: http://stackoverflow.com/a/26402542/3679676 ... or did you need it to run as-if it is a task in some other way? But that answer could also give you the clues you need. – Jayson Minard Sep 19 '16 at 14:59

6 Answers6

21

Thanks to the link how to run 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 that task

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

did the trick

Community
  • 1
  • 1
lapots
  • 12,553
  • 32
  • 121
  • 242
14

You can also use 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 your application as follows.

./gradlew run
Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77
Subash
  • 7,098
  • 7
  • 44
  • 70
14

In case you're using a Kotlin buildfile build.gradle.kts you would need to do

apply {
    plugin("kotlin")
    plugin("application")
}    

configure<ApplicationPluginConvention> {
    mainClassName = "my.cool.App"
}

If you apply the application plugin using the plugins {} block instead you could make it simpler:

plugins {
    application
}

application {
    mainClassName = "my.cool.App"
}

For details see https://github.com/gradle/kotlin-dsl/blob/master/doc/getting-started/Configuring-Plugins.md

Holger Brandl
  • 10,634
  • 3
  • 64
  • 63
6

For build.gradle.kts users

main file:

package com.foo.game.journey.ims.example


fun main(args: Array<String>) {
    println("Hello, world!")
}

build.gradle.kts file:

plugins {
    application
    kotlin("jvm") version "1.3.72" // <-- your kotlin version here
}

application {
    mainClassName = "com.lapots.game.journey.ims.example.ExampleKt"
}

...

And run with the following command:

./gradlew run
Lukas Kirner
  • 3,989
  • 6
  • 23
  • 30
5

I found another way from the @lapots answer.

In your example.kt:

@file:JvmName("Example")

package your.organization.example

fun main(string: Array<String>) {
    println("Yay! I'm running!")
}

Then in your build.gradle.kts:

plugins {
    kotlin("jvm") version "1.3.72"
    application
}

application {
    mainClassName = "your.organization.example.Example"
}
Jerv Norsk
  • 51
  • 1
  • 2
0

it is possible to do it without adding package name. Run your app with the green play icon and it should show the main class name at the end of the command it runs. enter image description here

inside build.gradle add className and application like this

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.5.10'
    id 'application'
}

mainClassName = 'AppKt'
...

now you can do ./gradlew run and to pass arguments also add --args='foo --bar'

George Shalvashvili
  • 1,263
  • 13
  • 21