29

In order to create my first, simple Kotlin project in Eclipse, I followed the steps in Getting Started with Eclipse Luna tutorial from Kotlin official website, i.e.:

  • Install Kotlin Plugin for Eclipse from Eclipse Marketplace
  • Open the Kotlin perspective and created a Kotlin Project
  • Created the file "hello.kt" inside the "src" folder with the main method
  • Right-clicked the file > Run As > Kotlin Application

However, I keep get the following error in Eclipse console:

Error: Could not find or load main class HelloKt

I have double-checked the Run Configuration for my project, and it does set the "Main class" as HelloKt (which I'm 100% sure it does not exist). Also, when I hit the "Search" button, the only item that Eclipse finds is HelloKt - (default package) (which, again, does not exist).

For the sake of completeness, find below the code for hello.kt file:

fun main(args: Array<String>) {
    println("Hello, World")
}

I noticed the following divergences between the tutorial and what I did:

  • I'm using a different Eclipse version (the tutorial suggests Luna; I'm using Mars.2).
  • I'm using a different JDK version (the tutorial DOES not suggest any specific version, but from the images, we can see they have used jdk1.6.0_45, while I'm using jdk1.7.0_79.

Note that I'm using the latest version of Kotlin Eclipse Plugin, so this is not the same case as in this post. These are the versions in my current installation:

  • Kotlin 0.7.2.v20160930-0748
    • kotlin-eclipse-policy 0.7.2.v20160928-1525
    • kotlin-weaving-feature 0.7.2.v20160928-1525

Can any of these divergences be the cause of the error while running the application?

Do you see/know any potential cause or known bug regarding this scenario?


UPDATE (May 14th, 2017)

Tried with Eclipse Neon.3 (eclipse.buildId=4.6.3.M20170301-0400), JDK 1.8.0_111, Kotlin 0.8.2.v20170314-0957 (kotlin-eclipse-policy 0.8.2.v20170314-0957, kotlin-weaving-feature 0.8.2.v20170314-0957, Equinox Weaving SDK 1.2.0.201701131634). The problem persists.

marcelovca90
  • 2,673
  • 3
  • 27
  • 34
  • Hi! I've just tried the same steps with jdk8 and I couldn't reproduce the problem. Could you please try to use jdk8? Also the recommended version of Eclipse is 4.6 (Neon), version that is on the website is outdated and will be updated soon. – Mikhail Zarechenskiy Nov 10 '16 at 13:32
  • 2
    Note that "HelloKt" is correct, this is how files with top level functions in Kotlin are translated into the class files. – Mikhail Zarechenskiy Nov 10 '16 at 13:36
  • Thanks for the suggestion. I changed to Eclipse Neon.1 (4.6.1) and jdk1.8.0_92, but got the same error. – marcelovca90 Nov 10 '16 at 23:54
  • I do think you should file a bug as all you've done seems reasonable, by the way mention your OS as the tutorial apparently uses Windows – niceman May 04 '17 at 12:07
  • Try using a newer version of Kotlin and the plugin, if possible or available. Try verifying whether the program compiles and runs through the command line either with a build tool or directly from the command line Kotlin compiler. – EvilTak May 05 '17 at 09:23
  • @EvilTak, I followed [this tutorial](https://kotlinlang.org/docs/tutorials/command-line.html) and was able to compile directly from the command line (using Kotlin compiler [1.1.2-2](https://github.com/JetBrains/kotlin/releases/tag/v1.1.2-2)). This reinforces my suspicion that there might a problem with the Eclipse plugin. – marcelovca90 May 05 '17 at 23:27
  • @niceman, when I posted the question, I tried those steps in Windows 10 Professional. Later, when commenting EvilTak's suggestion, I was in macOS Sierra 10.12.4. – marcelovca90 May 06 '17 at 16:37
  • This works for me. https://github.com/JetBrains/kotlin-examples First of all,you should run `mvn test` like compiler kotlin to java class – BirdZhang Jun 30 '17 at 10:13
  • Hi, got same issue on Eclipse Oxygen. When i use Neon, it was fine. I post the question here. https://stackoverflow.com/questions/46440028/kotlin-eclipse-oxygen-can-not-run-kotlin-app – suhao399 Sep 27 '17 at 14:48
  • 1
    set up a new workspace and try again. I fixed my issue – suhao399 Oct 10 '17 at 15:58

8 Answers8

4

Try new Kotlin File instead of Kotlin Class:

example

marcelovca90
  • 2,673
  • 3
  • 27
  • 34
  • Thanks for the suggestion, but unfortunately it didn't work. The same error occurred, i.e. `Error: Could not find or load main class HelloKt`. Tried with Eclipse Neon.3 (eclipse.buildId=4.6.3.M20170301-0400), JDK 1.8.0_111, Kotlin 0.8.2.v20170314-0957 (kotlin-eclipse-policy 0.8.2.v20170314-0957, kotlin-weaving-feature 0.8.2.v20170314-0957, Equinox Weaving SDK 1.2.0.201701131634). – marcelovca90 May 05 '17 at 01:10
  • Worked in Eclipse Oxygen – Venkata Raju Sep 24 '17 at 10:37
3

For me it happened only once. I think this is some glitch from the Kotlin plugin, when you first create a Kotlin file and try run it. Try "new Kotlin class" paste the same content and see if it works. After that you should be able to run both files as it happened to me.

Guilherme
  • 593
  • 1
  • 6
  • 23
  • Thanks for the suggestion, but unfortunately it didn't work. There was no `Run As > Kotlin Application` when I right-clicked the `Kotlin Class`. Tried with Eclipse Neon.3 (eclipse.buildId=4.6.3.M20170301-0400), JDK 1.8.0_111, Kotlin 0.8.2.v20170314-0957 (kotlin-eclipse-policy 0.8.2.v20170314-0957, kotlin-weaving-feature 0.8.2.v20170314-0957, Equinox Weaving SDK 1.2.0.201701131634). – marcelovca90 May 05 '17 at 01:09
  • @BashfulBeluga You should add Kotlin nature to your eclipse project. Project context menu (right click) -> Configure Kotlin -> Add Kotlin nature – ImGroot Sep 27 '18 at 19:55
1

I got same issues as you. I tried a lot of ways but not work (like uninstall plugin, then install plugin again, delete project and create new project...)

How to fix: I created a new workspace and setup the helloworld project again. I see that class file generated and I can run it normally.

Note that, we create new workspace and not project. What I notice is workspace contain .metadata folder that may have lot of information for Eclipse.

suhao399
  • 628
  • 7
  • 11
0

I had the same problem, it solved by deleting the JAVA_HOME environment variable.

I have set an old JDK folder that I deleted in the JAVA_HOME environment variable.

  • This is not a solution. At most, it is a workaround, but other applications may be affected by this removal. – marcelovca90 May 31 '17 at 19:32
  • Anyways, I tried both with and without JAVA_HOME environment variable, for both Kotlin File and Kotlin class, but it still didn't work. – marcelovca90 May 31 '17 at 20:27
0

Tried with Java 8 (1.8.0_144) and Eclipse Oxygen.1a Release (4.7.1a - build id 20171005-1200) with the following plugin versions:

  Kotlin    0.8.2.v20170314-0957    org.jetbrains.kotlin.feature.feature.group  JetBrains
    kotlin-eclipse-policy   0.8.2.v20170314-0957    org.jetbrains.kotlin.policy.feature.group   JetBrains
    kotlin-weaving-feature  0.8.2.v20170314-0957    org.jetbrains.kotlin.weaving.feature.feature.group  JetBrains

Everything worked as expected, without the need to create a separate workspace and/or use Kotlin File instead of Kotlin Class:

enter image description here

marcelovca90
  • 2,673
  • 3
  • 27
  • 34
0

This worked for me.... If you go to Window -> Show View -> Navigator It shows you more a physical layout of your workspace rather than the logical view by the Package Explorer. Right click the kotlin file with the main function defined and click Run As -> Kotlin Application. Worked for me, though I'd kill to be able to use a regular run config but such is life.....

MarkyMarksFunkyBunch
  • 1,060
  • 10
  • 10
0
  1. Go to Windows->show view->navigator
  2. Edit the .project file, changing just the following section to compile Kotlin file into same location as the Java files.
<linkedResources>
        <link>
            <!--name>kotlin_bin</name-->
            <type>2</type>
            <locationURI>org.jetbrains.kotlin.core.filesystem:/FindUniqueStrings/bin</locationURI>
        </link>
</linkedResources>

Keep in mind:

  1. I have commented out the name tag. Otherwise, Kotlin complier creates src folder kotlin_bin and puts your kotlin file in this folder and the kotlin runtime will not find it.
  2. The name of my java project is entitled FindUniqueStrings. Yours will be different.
  3. Double check that your java class files are complied into your project's bin folder by locating these files in the Navigator view.
robsiemb
  • 6,157
  • 7
  • 32
  • 46
0

I first compiled once with kotlinc in cmd, and then the issue resolved. kotlinc main.kt Next time, it will run with ctrl+f11 like normal java file. If you create a Kotlin file without a class it will run.

fun main() {
println("hi")
}
hamidreza75
  • 567
  • 6
  • 15