Omari Celestine
Omari Celestine

Reputation: 1435

Scala - IntelliJ IDEA Error: Could not find or load main class

I am currently using IntelliJ IDEA 2018.3.4 which I am trying to integrate with Scala but to no avail.

I have installed the Scala and SBT plugins and used the following steps in creating my project:

From there, the project is created. Following other guides, I then:

Code:

package main.scala

object Main {
  def main(args: Array[String]): Unit = {
    println("Hello World")
  }
}

But after right clicking in the editor and selecting Run Main, I get the following message:

Error: Could not find or load main class

Has anyone experienced this or can provide any solution?

Note

I have also tried creating a Scala application with sbt but the syncing process for the sbt dump never ends or gives an error and when I run the project, I get the same error.

Upvotes: 4

Views: 5642

Answers (2)

Chris Gong
Chris Gong

Reputation: 8229

When I experienced this issue after recreating your situation almost exactly step-by-step, what fixed the problem was putting the scala file, in your case Main.scala, in another package, named anything you want it to be, inside main/scala. Ultimately, the scala file's path would be something like

main/scala/<package-name>/Main.scala

Then, after fixing the package name in the scala file and running the scala file through IntelliJ, everything worked as it was supposed to.

Upvotes: 1

Vinh Truong
Vinh Truong

Reputation: 431

For Intellj IDEA, probably you will need to double check your module settings, and see if your scala folder is marked as sources root.

Check this URL for more details: https://www.jetbrains.com/help/idea/creating-and-managing-modules.html

Upvotes: 1

Related Questions