Scala Error: Could not find or load main class in both Scala IDE and Eclipse
Asked Answered
B

9

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.

Brandes answered 7/8, 2017 at 5:47 Comment(6)
I guess your tree structure of the project is wrong . it should be ScalaO/src/main/scala/com/ks/sparkscala/FriendsByAge.scalaJetton
So how should I modify the project structure?@Ramesh MaharjanBrandes
create those directories: main and scala inside src directory and just copy the package inside that. that should workJetton
It's worked. Thank you so much.@Ramesh MaharjanBrandes
my pleasure :) you can accept @Shankar's answer below in order to set this question as solved. :)Jetton
Changing the project structure as suggested by @Ramesh Maharajan worked for me as well. SCala project looks for main class inside src/main/java just like a typical maven projectMultivalent
C
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!

Chairman answered 7/8, 2017 at 6:6 Comment(2)
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 KoiralaBrandes
Just Misspelled problem, sorry to bother you again. I have already accepted your answer~ :) @Shankar KoiralaBrandes
F
4

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

It is working for me .

Fallacy answered 14/10, 2018 at 15:4 Comment(0)
R
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

Rauch answered 29/8, 2018 at 17:3 Comment(0)
E
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.

Emalee answered 29/11, 2018 at 15:54 Comment(0)
B
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

Baneberry answered 2/12, 2020 at 9:16 Comment(0)
E
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.

Equilibrate answered 31/7, 2019 at 15:25 Comment(1)
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.Toddler
G
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.

Glazier answered 15/10, 2019 at 11:22 Comment(0)
E
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.
Equilibrate answered 4/11, 2019 at 18:45 Comment(0)
A
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.

Alsup answered 26/10, 2020 at 19:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.