2

I am brand new to kotlin. I have installed kotlin plugins to eclipse. I found the simple example posted below in one of the tutorials. The issue is, when I run the project i receive the below stated error.

To solve this issue I tried to run the project as kotlin application, but I could not find that option at all.

please let let me know how to fix this error?

code:

fun main(args : Array) {
println("Hello, World!")
}

error:

Error: Could not find or load main class com.example.Main

update:

to solve this issue I followed exactly what is in this tutorial and I installed the most recent version of eclipse PHOTON but still the problem is there.

Simson
  • 3,373
  • 2
  • 24
  • 38
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • That code wouldn't even compile. You can't run code that doesn't compile. Look at the compilation errors, and fix them all. – JB Nizet Sep 15 '18 at 12:33
  • @JBNizet have a look at the update version please – Amrmsmb Sep 15 '18 at 13:32
  • Your code still doesn't compile, so you still can't possibly run it. – JB Nizet Sep 15 '18 at 13:34
  • That is not valid Kotlin code. Please look up some Kotlin tutorials better (at least one with correct Kotlin) than the one you posted on other comments. – m0skit0 Sep 15 '18 at 13:57

3 Answers3

1

if your main function is a top level function (not wrapped in a class or object)

then the generated class will be FIlenameKt, or for your case MainKt assuming it is in Main.kt

Nikky
  • 498
  • 2
  • 9
0

It's possible, that the .classpath file is wrong, maybe because you moved your project. I found a solution for your problem here:

  1. Delete .classpath and .project from your project
  2. Delete your project in eclipse. DO NOT check delete project contents on disk.
  3. Now, in a file explorer, go into $yourworkspace/.metadata.
  4. Search for $yourprojectname
  5. Delete everything you find. It should be safe-ish to delete anything in the .metadata directory.
  6. In eclipse: File > Import > General > Projects from Folder or Archive > $yourproject > finish
  7. Right click your project > properties > Java Build Path > Source tab
  8. Select all source folders, remove.
  9. Add folder, select src (whatever your src folder is called) and add it
  10. Go to libraries tab
  11. Add any jars to your build path here. There should be no more errors on your project now.
  12. Run your project like you normally would.

If you want to test your code, you can also do that online on the Kotlin website here.

I hope this could help you.

Thomi
  • 194
  • 1
  • 2
  • 12
0

The main function in Kotlin is different. You need to add <> wrapping the String to the function, and it should look like this:

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

Then your program should compile :-)

piotrek1543
  • 19,130
  • 7
  • 81
  • 94
Thomi
  • 194
  • 1
  • 2
  • 12