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?

2

6 Answers 6

20

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

2
  • Is there a way to get runtimeClasspath for apps with android application plugin? Feb 24, 2021 at 12:41
  • how to pass arguments to the main function? Aug 6, 2021 at 10:47
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
2
  • 2
    For someone having the same stupid problem as me: it should be apply plugin: "application" (note the : here!)
    – Michel
    Dec 6, 2019 at 10:44
  • it appears the colon is not in the answer.
    – Nailuj29
    Jan 13, 2021 at 17:02
13

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

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
1
  • this, rather than the apply { _plugin_} is what jetbrains is now pushing.
    – BobHy
    Nov 3, 2022 at 2:09
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"
}
1
  • Thank you for that post, really helpful :) Configuration is nice and clean :)
    – Hunter_71
    May 4, 2020 at 21:13
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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.