Kotlin Gradle Could not find or load main class
Solution 1
Change Applicationkt
to ApplicationKt
will work, and BTW you may upgrade Kotlin version to 1.3.50
.
By Applicationkt
I mean the one in this line:
attributes 'Main-Class': 'org.jetbrains.kotlin.demo.Applicationkt'
Solution 2
Kotlin compiles the Application file in two different files:
- one file called Application.class with the Springboot things
- another file called ApplicationKt.class with the main method
In this second file is where the main function is located at, so you have to use this name in the build.gradle file.
mainClassName = 'org.jetbrains.kotlin.demo.ApplicationKt'
Solution 3
Update your build.gradle to
jar {
manifest {
attributes 'Main-Class': 'org.jetbrains.kotlin.demo.ApplicationKt'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
with an upper case K in ApplicationKt.
This is required because of the way Kotlin compiles to Java Bytecode. The fun main()
function in Kotlin is not attached to any class, but Java always requires a class and does not support classless functions.
The Kotlin compiler has to create a Java class. Because you already defined a class Application
it created one with the suffix Kt
for the functions in your Kotlin file org/jetbrains/kotlin/demo/Application.kt
. You have to set this class so that the JVM can find it.
BTW a Jar file is just a Zip file, you can unpack it and see for yourself if the ApplicationKt.class is there.
dks551
Updated on July 12, 2022Comments
-
dks551 almost 2 years
I tried to copy the Spring Boot Kotlin sample project https://github.com/JetBrains/kotlin-examples/tree/master/tutorials/spring-boot-restful. I Added some more dependencies and when I tried to build the executable jar and run it, I got the error:
Could not find or load main class...
Gradle build script:
buildscript { ext.kotlin_version = '1.1.3' // Required for Kotlin integration ext.spring_boot_version = '1.5.4.RELEASE' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Required for Kotlin integration classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version" } } /*plugins { id 'org.springframework.boot' version '2.0.0.RELEASE' }*/ apply plugin: 'kotlin' // Required for Kotlin integration apply plugin: "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin apply plugin: 'org.springframework.boot' jar { baseName = 'gs-rest-service' version = '0.1.0' from { (configurations.runtime).collect { it.isDirectory() ? it : zipTree(it) } } manifest { attributes 'Main-Class': 'org.jetbrains.kotlin.demo.Applicationkt' } } sourceSets { main.java.srcDirs += 'src/main/kotlin/' test.java.srcDirs += 'src/test/kotlin/' } repositories { jcenter() } dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // Required for Kotlin integration compile("org.springframework.boot:spring-boot-starter-web") compile group: 'org.apache.camel', name: 'camel-quartz2', version: '2.20.2' compile group: 'org.apache.camel', name: 'camel-http4', version: '2.20.2' compile group: 'org.apache.camel', name: 'camel-docker', version: '2.20.2' compile group: 'org.apache.camel', name: 'camel-aws', version: '2.20.2' testCompile('org.springframework.boot:spring-boot-starter-test') }
-
ice1000 about 6 yearsI think the first two paragraphs are irrelated
-
phisch about 6 yearsUpdate the answer to explain why Kotlin does require the additional
Kt
-
dks551 about 6 yearsI changed the extensions to Kt and also upgraded the kotlin version to 1.2.31, but no luck. @phisch Can you try by yourself building a fat jar for the above project with additional dependencies.
-
phisch about 6 yearsI built a 'Hello World' example and it runs as expected. Please update your question with more information, especially the exact error message, the contents of your jar file (is there a class ApplicationKt), etc. Thank you.
-
dks551 about 6 years@phisch Did you run ./gradlew clean jar command ? And after that you run the standalone jar file ? Can you try with the above github repository link ?
-
phisch about 6 yearsYes I did and a spring boot applications starts. My build.gradle is slightly different though, see updated answer. We will need more details from you (see my previous post) to help you. Thanks.
-
phisch about 6 yearsDid you have any success or are there still open questions?