NoClassDefFound with Kotlin-Java project

I’ve simple Kotlin project with structure like that:

enter image description here

I want to run application with java class named TestClass

public class TestClass {

private String name;
private String age;


TestClass(String n, String a){name = n; age = a;}

public String getName() {
    return name;
}

public String getAge() {
    return age;
}

}

In Kotlin file:

    fun main(args: Array<String>) {

    val javaClass = TestClass("Miko","18");
   println(javaClass.name)

}

Compilator sends me following error:

Exception in thread "main" java.lang.NoClassDefFoundError: TestClass
at MainKt.main(main.kt:4)

Caused by: java.lang.ClassNotFoundException: TestClass
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more

What’s causing that problem?

Looks more like a runtime error. Correct me if I’m wrong but I expect you can compile the program but it fails when you try to run it.
Can you explain how you started the program when you got that error?

Thank you for your answer. Yes I can compile the program. I’m new in Kotlin, this is brand new project . I tried to test some examples that cover Kotlin-Java and then got this error. Project contains only default files + stuff I’ve covered in post.

That wasn’t my question.

Do you use intellij to start the program or do you run it from the command line (if so what command do you use exactly). Also it might help if you post your build.gradle.kts file.

I’m using Intellij to start the program.

build.gradle.kts here:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

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

group = "me.miko"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(kotlin("test-junit"))
}

tasks.test {
    useJUnit()
}

tasks.withType<KotlinCompile>() {
    kotlinOptions.jvmTarget = "1.8"
}

application {
    mainClassName = "MainKt"
}

I can’t really think of anything that looks odd. You could try moving the java class from src/kotlin to src/java but I always thought that this didn’t matter.
Another thing you could try is to start the application from the command line (gradlew run). That way you know if it’s an intellij or a kotlin issue.
If nothing helps you can share your project here (github or something similar is great) and we can try to replicate the issue. In that case you should also create an issue at https://kotl.in/issue (kotlins bug tracker) and link the project there as well.

Thank you for all your commitment. Unfortunately gradlew run didn’t help.
Here’s link to GitHub repository: GitHub - amtrax325/no-class-def-found-kotlin-issue

Looks like moving your java source file from src/main/kotlin/TestClass.java to src/main/java/TestClass.java solves your problem. For some reason I though you could also have your java source files in the kotlin folder but it looks like I’m wrong. I also tested this using kotlin 1.3.72 and got the same result so this is not a change in 1.4 that I just missed.

2 Likes

They have to be in separate directories by default, but this can be configured in build.gradle.kts, see:

1 Like

I believe the advice is to place mixed Kotlin and Java source code under the src/main/Java folder together (not separating them) but I could be wrong.

I know back in the early days, I read that was the recommendation somewhere.

1 Like

Yeah, I also can’t remember but I’m sure there was something about putting them together into the same folder. That’s why I didn’t pick up on this earlier.

1 Like

Thanks for the link. I didn’t find that one earlier (only finding a somewhat outdated page which was about the intellij internal build system).

Kotlin sources can be stored with Java sources in the same folder, or placed to different folders. The default convention is using different folders:

  • project
    • src
      • main (root)
        • kotlin
        • java

This is somewhat misleading though. It sounds like it doesn’t matter where you put the java or kotlin files as can be in in either folder and it is just recommended to split them up. Is that just me reading this that way?
I know the next line qualifies that somewhat but I’m not sure it is enough:

The corresponding sourceSets property should be updated if not using the default convention:

Anyone else thinks this should be adjusted? If so I can write a quick PR to change it.

2 Likes

On a second look at the “Using Gradle” page: Are there 2 blocks with the title " Kotlin and Java sources" both with somewhat different explanations? Also the second block is under " Targeting JavaScript".
I guess I will have to write a PR anyways :wink:

1 Like