简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM