3

Some time ago I created a gradle-based project in IntelliJ to play around. I added a few Kotlin and java classes in "main", and a single java class in "test".

Now IntelliJ suggests that I can run that test class. But there is no option to "run" a new Kotlin main() that I just added in a new .kt file in the main section.

Things compile fine, but no indication whatsoever that I could "run" that main method.

I see this question, but that is for a pure Kotlin project.

I had a look into "Run/Debug" configurations. In the templates, there is one Kotlin. When I use that, I can use the file chooser to look at my Kotlin classes; down to the main method. But the "OK" button is disabled, so I can't select anything as "run" target.

I figured that I can run one of my java classes by adding this task

task execute(type:JavaExec) {
    main = 'com.whatever.Hello'
    classpath = sourceSets.main.runtimeClasspath
}

and then I can run gradle[execute]. But When I point main to one of my Kotlin classes, I get

Task :execute FAILED

Error: Could not find or load main class com.whatever.TheKotlinClass

2 Answers 2

2

You have to set main = 'com.whatever.TheKotlinClassKt'. The Kotlin compiler changes the name of compiled classes.

See for more details: What is the reason for using “Kt” suffix in Kotlin classes?

Sign up to request clarification or add additional context in comments.

1 Comment

For me, there was a bit more to it (which I will add in my own answer), but your input had (in the end) the desired effect.
1

After a lot of trial / error, I figured that my gradle script was missing various important things. Besides that, I also had forgotten to mark the kotlin folder as "source folder" in IntelliJ (as the second answer on the SO question linked in my question suggests to do).

For reference, here is a working version of my gradle script:

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.3.31'
}

version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile "org.mockito:mockito-core:2.+"
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    compile "org.jetbrains.kotlin:kotlin-stdlib-common"
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

task execute(type:JavaExec) {
    main = 'com.whatever.TheKotlinClassKt'
    classpath = sourceSets.main.runtimeClasspath
}

And, side note: as soon as I found out how to correctly compile the Kotlin files within the gradle project ... the IntelliJ IDE also shows the "Run" button for every Kotlin class with a main().

5 Comments

as you created your project some time ago... maybe you want to create a new one and check the "Kotlin DSL build script"-checkbox? (of course, that wouldn't have solved your problem... but you get type safety in your build script if you want ;-))
@Roland Can I add that somehow to my existing project?
yes, at least by hand ;-) it's named build.gradle.kts and everything in there is Kotlin... in the end it also looks very similar to the build.gradle itself... I would probably just generate a single project with the kotlin dsl enabled and use that build.gradle.kts as a blue-print...
actually I find that problem/behaviour quite ~special... if you just start a new project/module all files in src/main/kotlin/ or src/main/java/ usually are directly runnable... the same with the test-counterpart... the other topic is gradle itself... maybe a tag regarding it might be worth it... what I also wondered is, that you needed to specify that it's a source folder.. that isn't really necessary usually... so: if I don't really understand why a certain error happens and why that answer then lead to the solution, it's also hard to upvote it... (at least for me)
regarding the accepted answer... actually... without seeing your file containing the main there is still a chance, that what you have written could be correct, e.g.: using something like @file:JvmName("Hello") in the file that contains the main or by placing the main inside an object with @JvmStatic... not that I recommend any of those... but... it could be...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.