Scala project won't compile in Eclipse; "Could not find the main class."

Thomas Heywood picture Thomas Heywood · Oct 17, 2010 · Viewed 38.8k times · Source

I have installed Eclipse 3.5.2 and today's Scala plugin from /update-current (that's Scala 2.8 final.) I can compile and run Scala projects consisting of a single singleton object that implements main().

But, if a project contains more classes, I receive the "Could not find the main class" error.

I have tried searching for the solution and I discovered:

Eclipse is correctly looking for the Main$ class, not the Main class
* under Debug Configurations, my main class is correctly identified as mypackage.Main
* my plugin is up to date and recommended for my version of Eclipse
* cleaning, restarting etc. doesn't help.

The same project will compile with scalac.

Thanks for any ideas on how to solve this.

EDIT: MatthieuF suggested I should post the code.

This snippet produces an error. It's not the most idiomatic code, but I wrote it that way to test my environment. I tried it as a single file and as separate files. It DOES work with scalac.

import swing._

class HelloFrame extends Frame {
        title = "First program"
        contents = new Label("Hello, world!")
}

object Hello {
  val frame = new HelloFrame    
  def main(args : Array[String]) : Unit = {
        frame.visible = true
   }
}

BUT, if I nest the definition of HelloFrame within Hello, it works. This snippet runs perfectly:

import swing._

object Hello {

    class HelloFrame extends Frame {
        title = "First program"
        contents = new Label("Hello, world!")
    }

    val frame = new HelloFrame

    def main(args : Array[String]) : Unit = {
        frame.visible = true
    }
}

Answer

ninjagecko picture ninjagecko · Jul 1, 2011

For me, the problem was that there was a build error (see Problems tab) which was preventing compilation; oops! The reason you see the error is that the run macro proceeds despite the failed compilation step, and attempts to run class files it expects to be there; they don't exist because there was a build error preventing compilation, so it says it can't find Main (not compiled).

Problem goes away when build can complete successfully, i.e. errors are fixed.

I guess, theoretically, there may be more complicated reasons your build is not completing successfully that are not listed in Problems.