Eclipse will not build a kotlin project

I am unable to get eclipse to compile and run a Hello world Kotlin application. Below are the steps I took:

  1. Create a new Kotlin project called kotlin-test using the UI wizard
  2. Create a new Kotlin file mypackage.MyClass with a main method to print helloworld
  3. Attempt to run it by right clicking and hitting run as Kotlin application

The result of running is the following error:
Error: Could not find or load main class mypackage.MyClassKt

The generated launch config has the following setup:

  • Project is kotlin-test
  • Main class is mypackage.MyClassKt
  • Classpath contains JRE Library and a user entry called kotlin-test (which includes the kotlin runtime library)

Project Setup Details:

  • Java Build Path has kotlin-test/src
  • Libraries contains JRE and Kotlin Runtime Library
  • Project Natures has Java and KotlinNature
  • The Kotlin Runtime Library contains 4 libs (kotlin-stdlib, kotlin-reflect, kotlin-script-runtime, annotations-13.0). There is also a reference to a kotlin_bin which contains mypackage with MyClass.class and MyClassKt.class. These don’t actually exist on my filesystem however (search everything doesn’t find them).

In the Kotlin Runtime Library I can right click on kotlin_bin and here are it’s properties:

  • Path: /kotlin-test/kotlin_bin
  • Type: Linked Folder
  • Location: \kotlin-test\kotlin-bin
  • Resolved location: \kotlin-test\kotlin_bin
  • Last Modfied: December 31 1969 at 7:00:00PM

In the edit link dialog it says the resolved location is C:\kotlin-test\kotlin_bin. This is wrong (KE-276). I pointed it to PROJECT_LOCATION\kotlin_bin based on the suggestion in the issue but it still does not work.

Versions:
Eclipse 20180917-1800
Kotlin 0.8.13.v20190205-1149

Hello,
if you have both MyClass.class and MyClassKt.class in kotlin_bin directory it seems that you defined a MyClass class inside MyClass.kt file. Is main function visible in kotlin_bin > MyClassKt.class dropdown?

Thanks!

Hello - I do not have any class files in kotlin_bin.

At the moment it is pointed to C:\kotlin-project-test (which is the same as project directory). Pointing it to C:\kotlin-project-test\bin does not work either.

Here is MyClass.kt:

package mypackage

open class MyClass

fun main(args: Array) {
System.out.println(“Hello World”)

}

kotlin_bin:
image

image

filesystem:
image

Ok, there are things to untangle here, but also one that concerns me.
First, kotlin_bin directory should not exist on drive and this is intentional - this is a virtual place for light classes generation only. Making it pointing to a particular place on drive can mess things up.
The code above should not compile, as the Array is a generic and the eclipse should show an error here. If it does not, I am worried. Yet, even with such errors, you can run the application with sources compiled at the moment when everything was compilable. And the run-time configuration can be not up to it, causing aforementioned error.

Where are the compiled class files supposed to go? I made another attempt this time without modifying kotlin_bin. There are “class files” listed under kotlin_bin but they don’t actually exist on the file system which leads me to believe eclipse is never compiling the Kotlin code. The only thing in my bin directory is MyClass.kt (which is suspicious… why is the source code being placed in bin?)

image

The editor highlighter works correctly and is highlighting both invalid code and unknown objects. It is able to suggest imports which means it can understand the context as well.

As for the code in the example it compiles and runs fine in idea.

Source code being moved to bin folder is known error and will be fixed in next release. Currently, the .class files are generated only before app launch (see bug KE-57 for more details), in other cases only virtual light classes are being created.

Under ‘kotlin_bin’ dropdown you have MyClassKt.class now. Are you still experiencing the error?

It does not work. Below is the config it generated when I right clicked on main and hit Run As > Kotlin Application

Launch command line:

C:\Program Files\Java\jdk1.8.0_101\bin\javaw.exe -Dfile.encoding=Cp1252 -classpath "C:\kotlin-test4\bin;C:\kotlin-test4\kotlin_bin;C:\eclipse-2018-09\configuration\org.eclipse.osgi\800\0\.cp\lib\kotlin-stdlib.jar;C:\eclipse-2018-09\configuration\org.eclipse.osgi\800\0\.cp\lib\kotlin-reflect.jar;C:\eclipse-2018-09\configuration\org.eclipse.osgi\800\0\.cp\lib\kotlin-script-runtime.jar;C:\eclipse-2018-09\configuration\org.eclipse.osgi\800\0\.cp\lib\annotations-13.0.jar" mypackage.MyClassKt

image

image

image

Could you please upload zipped project with your code?

Attached though it’s nothing special. Also is there a chance the Kotlin plugin doesn’t play well with other plugins?

kotlin-test4.zip (2.5 KB)

Installation details:

After a lot of experimentation here is what I have found:

  • Reproducing the issue seems to require more than one project in the workspace. I could run it fine if I created a new workspace with just the one project. After importing in my other 100 projects it stopped working.
  • I was unable to pinpoint if a specific project was causing the issue. I experimented adding certain projects and removing others but could not find anything conclusive.
  • The issue still occurs even if I close all of my other projects
  • For some reason after experimenting with adding/removing projects for some time I got one of my test workspaces working with all the projects loaded. My real workspace still does not work.

I compiled 0.8.13.5 and threw in some println’s. I was able to determine that KotlinBuilder.build is not always called when it should be (based on comparing the behavior of the plugin on a separate workspace where I got a kotlin project working). It is called when the project builds. It is not called right before launching the app. As a consequence the lightweight classes are never compiled with compileKotlinFiles. This explains why the “class” files exist under kotlin_bin but never make it on to the filesystem.

The kotlin nature and builder are applied and the builder is first in the list. I’ve run up and down the config including doing file diffs on internal metadata files between the case that works and the case that does not work and I could not find any differences. I do not know why this is happening. Here is a rambling of some theories:

  • It could be because of having multiple projects in the workspace
  • It could be having multiple projects in the workspace increases the chances of the issue showing up
  • It could be because other projects have different natures
  • It could be because of how the projects land (sort order, load order, something else?)
  • It could be because some registration logic somewhere is flaky
  • It could be building the project leaves eclipse in an unexpected state
  • It could be because some kind of delta logic thinks there are no changes to no build needs to be attempted because launching the project
  • It could be because the launch mechanism being used is the wrong one.

We have a really hard time trying to replicate your issue, but I have some idea. Could you please install plugin from this update site: https://teamcity.jetbrains.com/guestAuth/repository/download/Kotlin_KotlinEclipse_EclipsePluginDev/2059552:id/ and let us now if it changes anything. It is shot in the dark but it may give us some insight.

Unfortunately it didn’t work. I tried removing and readding the nature as well as creating a new configuration. From the eclipse logs (with debug on) I can see it still is not running the builder before launch (there are no logs indicating the builder was asked to run).

I have eclipse build logs on debug with the following .options file:
org.eclipse.core.resources/debug=true
org.eclipse.core.resources/build/deltra=true
org.eclipse.core.resources/build/failure=true
org.eclipse.core.resources/build/interrupt=true
org.eclipse.core.resources/build/invoking=true
org.eclipse.core.resources/build/needbuildstack=true
org.eclipse.core.resources/build/stacktrace=true

I was facing similar issue. Class path problem due to kotlin_bin. First time I created Kotlin project and tried to create ‘app.kt’ as mentioned in tutorial. But the editor was not able to open ‘app.kt’. The class path problem due to kotlin_bin listed in Problem tab. I removed the project and created again (no .kt file created) and restarted the Eclipse.The problem was showed up just after restarting Eclipse. It’s just an information which may help to investigate further.

I am new to Kotlin. Your thread help me to understand that the problem is not common. I downloaded latest Eclipse and installed Kotlin as mentioned in Kotlin tutorial and the issue didn’t appear. Due to time constraint, I did not spend time to understand the root cause.