Michiel Leegwater
Michiel Leegwater

Reputation: 1180

Kotlin and Java project in Eclipse, NoClassDefFoundError, where are the class files?

In this question the basic configuration of Kotlin + Java + Gradle in Eclipse is described. It allows me to create Kotlin code. The Kotlin and Java natures are correctly present. Unfortunately, the code does not run. Not as Kotlin application nor as JUnit test.

When my (i.e. created by me) Kotlin class is called from Java code, it yields a NoClassDefFoundError. When the same test/application is run from Gradle or IntelliJ the code runs correctly.

My investigation thus far has uncovered that the class files in the bin folder are not there to be found when executing.

Using the buildship plugin

The image below shows that the src tree exists. All Java compile classes are in the src tree. The kotlin_bin folder has the correct folders, but no files.

Eclipse bin folder

Using gradlew eclipse and import .project files

This results in the same behavior. I prefer buildship to manage gradle.

Tool versions

I'm using:

Any tips on how to proceed? I would like to be able to run in Eclipse. We're adding a small Kotlin part to the project, I would prefer not to force my team to switch to IntelliJ.

Upvotes: 8

Views: 2248

Answers (3)

Jerry Tom
Jerry Tom

Reputation: 3

since i branch my project ,then refactor project name suit next feature. then my project can't find kotlin class.i search google and stackoverflow no effect. i diff project with scm HEAD version, discover the differece in

.project

<linkedResources>
    <link>
        <name>kotlin_bin</name>
        <type>2</type>
        <locationURI>org.jetbrains.kotlin.core.filesystem:/**YOUR_PROJECT**/kotlin_bin</locationURI>
    </link>
</linkedResources>

then i fix project name in .project file. it was fixed after rebuild

Upvotes: 0

Michiel Leegwater
Michiel Leegwater

Reputation: 1180

This issue seems to be an issue with the Eclipse plugin. See KE-344. All symptoms seem to match.

Upvotes: 2

hfontanez
hfontanez

Reputation: 6188

Typically, when you get a NoClassDefFoundError it means that the class loader cannot find your classes because your class "disappeared". In Eclipse, this is typically fixed with a simple "Clean Project" to force rebuilding. On the other hand, ClassNotFoundException means something went wrong with your classpath. If cleaning the project and rebuilding doesn't work, I think it might still be possible something could be wrong with your classpath. To check your classpath in Eclipse, change to the Navigator View and open your .classpath file (see image below).

Navigator View

Then, check your classpathentry nodes to see "output" path. Your .classpath should contains mappings for your source location, your output location (where compiled classes reside), as well as references to your JRE container and any libraries imported by the project. Make sure all of these entries are correct.

My guess is that your classpath is not right. To fix, follow these instructions: https://javarevisited.blogspot.com/2011/06/noclassdeffounderror-exception-in.html. One of those suggestions should work.

Upvotes: 0

Related Questions