15

Here is my problem, I know there are lots of answers for similar questions, however none of them worked after I tried. I'm using both Scala IDE 4.6 and eclipse Oxygen to run the code and all failed on this error.

Here's my scala compiler configuration:

scala compiler configuration

Here is my run configuration:

run configuration

Here is my code, file structure and error showed in console:

enter image description here

Here is the information Problem console:

enter image description here

From online answer, I have already tried to clean the project before building, I also tried all the versions of JVM and Scala compiler, all of those didn't help.

The code was directly import from a online course code, so I believe there shouldn't be any errors in the code.

halfer
  • 19,824
  • 17
  • 99
  • 186
SKSKSKSK
  • 545
  • 1
  • 4
  • 15

9 Answers9

22

I think this should be your folder structure

Scala0
└── src
    └── main
        └── scala
            └── com.ks.sparkscala
                └── FriendsByAge

In your case create a folder main/scala inside src and copy the package inside it.

Please follow here for the project structure

I hope this helped!

koiralo
  • 22,594
  • 6
  • 51
  • 72
  • I thinks I'm happy too early. once I import another scala file as Scala0/src/main/scala/com/ks/sparkscala/another.scala, the same problem happened again. Ps: this scala file has its own main function in the scala object.@Shankar Koirala – SKSKSKSK Aug 07 '17 at 06:58
  • Just Misspelled problem, sorry to bother you again. I have already accepted your answer~ :) @Shankar Koirala – SKSKSKSK Aug 07 '17 at 07:11
4

In the eclipse -->project properties-->scala compiler ---> change the scala installation.

It is working for me .

1

For Intellij IDE, use the following procedure:

  1. Go to the drop down at right top of window and select "Edit Configuration".

  2. On the pop-up screen, select "Scala-console" in left side, and in the right side, select your module name under "Use class path and SDK of module.

enter image description here

KayV
  • 12,987
  • 11
  • 98
  • 148
1

As far I can see your package structure is com.ks.sparkscala which is different from the declared one in FriendsByAge.scala, com.sundogsoftware.spark Hope this helps.

barbara
  • 51
  • 4
1

For the reference of the IntelijIDEA I guess somehow you would be using the scala plugin, in that case, there is a fix to the above-mentioned question:

Error: Could not find or load main class in both Scala IDE https://youtrack.jetbrains.com/issue/SCL-13636

Try to reimport the project and overwrite the .idea directory as while importing as a Existing Project it will prompt to overwrite the .idea configuration. Hope that Fix

FYI Its a UNRESOLVED issue

initvik
  • 91
  • 1
  • 5
0

I had a similar problem. I’ve got Could not find or load main class in IJIdea IU-182.4892.20 / Scala 2.13 for .scala file with such structure:

package ...

import ...

object Main extends App {
...
}

//other traits, classes, objects

But when I moved //other traits, classes, objects to the separate .scala file in the same package my problem was solved.

Dedkov Vadim
  • 426
  • 5
  • 10
  • Thank you! In my case i tried to create my own monad types inside extendsApp class. I moved entire implementation of the class to another class and left only the test logic and everything became to work. – S.Daineko Sep 21 '19 at 09:35
0

I was experiencing the same error message and the below solution worked for me:

  1. Right-click on the project.
  2. Click on Scala.
  3. Choose the option "Set the scala installation".
  4. Select the Scala Installation (I selected "Fixed Scala Installation: 2.10.6(bundled").

After this, it ran fine and did not give that error message.

Saurabh Rana
  • 168
  • 3
  • 22
0

Another one solution. I had a similar problem on project with Scala + SBT.

How I solved this one:

  1. Run my project via SBT like this: sbt my-project/run
  2. Stopped it (Ctrl+C)
  3. Run debug -- it works again.
Dedkov Vadim
  • 426
  • 5
  • 10
0

There were numerous things I need to solve this problem, and I think every one of them was important.

  1. Make sure the scala file in question is in the "src" folder.
  2. Make sure it is an object (if your inner class/object is an object).
  3. Make sure the filename and the class/object name is the same.

(The following instructions work in IntelliJ)

If #1 is not true, here's the easiest way to fix it:

  1. Click and drag your file into the "src" folder in IntelliJ. Say "yes" to any refactorings.

If #2 or #3 is not true, here's the easiest way to fix it:

  1. Copy your code (CTRL+A, CTRL+C)
  2. Right-click your "src" folder and select "New Scala class"
  3. Type in the name of your class / object
  4. Select "Object" if your file will contain a top-level object instead of a top-level class
  5. Press ENTER (or select the appropriate confirmation button)
  6. Paste in your code (CTRL+A, CTRL+V);

RESULT: Now you should have a file called "YourClass.scala" in your "src" folder, and it should have an orange icon if it's an object. The filename should match the name of the toplevel class/object. And you should be able to right-click it and run it. And then after you do that, you should be able to just press the play button in the top right corner. And it should work.

shieldgenerator7
  • 1,507
  • 17
  • 22