Lucas Bali
Lucas Bali

Reputation: 81

Problems compiling and running Scala projects in IntelliJ Idea

I have installed the latest IntelliJ Idea vertion with the Scala plugin. I have created a SBT Scala project with the IDE. Unfortunately, I am not able to compile or run even the simplest "Hello World" example.

object Main {
    def main(args: Array[String]): Unit ={
        val x: Int = 5
        println("Hello Scala!")
        println(x)
    }
}

If I try to run it, it says that "Error: could not find main class Main". Rebuilding the project does not help.

BUT, if I run the "sbt" terminal program and execute "run", everything runs fine. Even more, it compiles the necessary class file so that IntelliJ Idea is able to run it after this step. But, whenever I change something in the code and try to rebuild it from Idea, it will fail as before.

Edit: in Eclipse everything runs ok.

UPDATE

If I try executing "compile" from the sbt shell and then "Run" with the IDE, it will work. But, it will not do it while executing "Build" or "Rebuild Project". Sometimes, but unfortunately not always sp as to be able to reproduce it, it will throw me an exception with "Could not initialize class sbt.internal.io.Milli$"

UPDATE 2nd:

I have simplified even more the task. Now I have something like the following screenshot:

Screenshot IntelliJ IDEA

UPDATE 3rd:

There were two errors in my deployment. One of them was that, as it was pointed before, there was some package definition problems. All the source code should depend on src/main/scala, but that is not enough. So as to use the SBT structure for construction, I had to go to File -> Settings -> Build, Execution, Deployment -> Build Tools -> sbt and check "Use auto-import" and "use sbt shell for build and import".

After that, everything runs ok. Finall!

Thanks a lot tro everyone for the useful input!

Upvotes: 2

Views: 2933

Answers (2)

Dmytro Mitin
Dmytro Mitin

Reputation: 51703

On your screenshot Runner is in wrong package. It's written package main.scala. If Runner.scala is in src/main/scala/way/to/my/package package declaration should be package way.to.my.package (if it's just in src/main/scala there should not be line package ...).

Also if still necessary you can try (from what should be tried first to what should be tried last, if things tried before didn't help)

  • sbt clean
  • reimport the project to IntelliJ IDEA
  • File -> Invalidate Caches / Restart ...
  • delete .idea subfolder of project folder

Upvotes: 3

Donat
Donat

Reputation: 4833

Did you try the green arrow near "object Main"? This should start the program.

Screensht from IntelliJ Idea

Upvotes: -1

Related Questions