Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • File > Create New Project
  • Selected Java from the left
  • Selected Scala from the right > Next
  • Entered the name of the project > FINISH

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

  • Right click on the src directory
  • New > File
  • Enter main/scala/Main.scala
  • And enter the following code in there:

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.

like image 707
Omari Celestine Avatar asked Oct 20 '25 01:10

Omari Celestine


2 Answers

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

like image 178
Vinh Truong Avatar answered Oct 21 '25 15:10

Vinh Truong


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.

like image 36
Chris Gong Avatar answered Oct 21 '25 13:10

Chris Gong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!