user6186835
user6186835

Reputation:

Kotlin And Java In The Same Project Using Eclipse IDE

I posted a similar question regarding gradle but this question is without gradle or maven.

I can not get Kotlin working properly using Eclipse IDE. This works great using IntelliJ, however many developers still use Eclipse. I have installed the Kotlin Eclipse plugin and does not work. I have downloaded the Kotlin standard library and runtime library and added them into the project. Still not working. All I get in eclipse when I have Java and Kotlin is cannot be resolve to a specified type.

I'm not using maven or gradle because I couldn't get it working with those two either.

If I mix Java and Kotlin in the same source folder I get this error. "The type error.NonExistentClass cannot be resolved. It is indirectly referenced from required .class files"

I'm using Eclipse Neon. If anyone can help me that would be awesome, I've been trying for quite some time now and not getting anywhere. :(

enter image description here

Upvotes: 20

Views: 10423

Answers (6)

laloumen
laloumen

Reputation: 1369

If your project was like mine, it has .kt files in /src/main/kotlin, some missing references in Java. I tried compiling them but nothing worked. It turns out that my project didn't have an Eclipse Source Folder associated with the kotlin code. There were the usual ones for "src/main/java", "src/main/resources" but not one for "src/main/kotlin".

So, I created a source folder for the kotlin files.

  1. Right click the project
  2. New "Source Folder"
  3. Specify folder name: "/src/main/kotlin"

This doesn't create anything in the file system but just creates a logical container for Eclipse to work with the contents. In this case, Eclipse recognized the .kt files, compiled them and all the missing references issues all cleared up.

Upvotes: 0

I'm working on a project with Spring Boot and Kotlin (some controllers/mappers/classes in Java and others in Kotlin) and after trying a lot of approaches, the only that worked was to use Eclipse 03-2020 and Kotlin Plugin for Eclipse V0.8.19.

https://dl.bintray.com/jetbrains/kotlin/eclipse-plugin/0.8.19/

  • Before everything, close your project and uninstall the previous version of Kotlin Plugin for Eclipse.
  • Go to Help/Install New Software.
  • Copy the link of Eclipse Plugin and continue with the installation (do not forget to check all the options to install).
  • After the installation restart the IDE and try compile again.

Upvotes: 0

Alex R
Alex R

Reputation: 11893

As of the current Eclipse version (2019-09):

You can't add Kotlin to a Java project, but you can add Java to a Kotlin project.

The procedure to accomplish a mixed Kotlin/Java project was roughly:

  1. Install Kotlin plug-in
  2. Create empty Kotlin project
  3. Move the Java code into the Kotlin project
  4. Delete the original project
  5. Fix project references

Upvotes: 0

Suula
Suula

Reputation: 46

Got similar issue solved by adding a new Kotlin file to a Kotlin/Java mixed project. Adding the file caused Eclipse 2018-09 (4.9.0) to add kotlin-stdlib.jar and kotlin-reflect.jar to classpath and everything started working.

Upvotes: 1

user6186835
user6186835

Reputation:

Add Kotlin Nature fixes the issue. Click on your project and Configure Kotlin -> Add Kotlin nature

This partially fixes the issue, though eclipse plugin is still buggy and auto import function still doesn't work for me.

If you're having any issue, make sure you have kotlin_bin folder added in your project. Also make sure that ALL kotlin files have the correct package name sometimes when you rename packages or move files around kotlin classes may not get updated.

Upvotes: 16

user2718311
user2718311

Reputation: 9

Add Kotlin Nature fixes the issue. Click on your project and Configure Kotlin -> Add Kotlin nature

Upvotes: 0

Related Questions