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

12

24

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
    }
}
Kiaochow answered 17/10, 2010 at 13:46 Comment(1)
Can you post your code please?Lakenyalaker
R
21

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.

Righteous answered 1/7, 2011 at 21:8 Comment(2)
Doh! I totally missed looking in the Problems view when I had this problem. Thanks for the reminder.Otto
This worked for me as well. I had a classpath issue that was preventing the Scala plugin from finding my main class for some weird reason. All good now!Sixteenmo
L
5

One possibility is that you are trying to launch using ctrl-F11, but from a different class.

The Scala Eclipse plugin does not obey the defaults for Java launching. In Preferences->Run/Debug->Launching, there are some options Launch Operation->Always Launch the previously selected application, etc. This currently does not work in the Scala eclipse plugin. To launch a specified main, you need to launch it from the editor for the class.

There has been a bug raised for this. http://scala-ide.assembla.com/spaces/scala-ide/tickets/1000023-scala-launch--does-not-follow-jdt-behaviour

EDIT: This is now (mostly) fixed.

Lakenyalaker answered 17/10, 2010 at 14:28 Comment(1)
I'm launching with Alt-Shift-X, S or alternatively by right-clicking the object implementing main() in the Package Explorer and selecting Run As/Scala Application. Unfortunately,the problem remains.Kiaochow
C
2

I'd solve similar problem by executig "Project->Clean.." with next automatically building.

Confraternity answered 2/3, 2015 at 7:22 Comment(0)
H
1

I had the same error message with a Java application made by myself.

The problem was that I deleted (though inside Eclipse) a jar that belonged to the Java build path, without deleting it from the Java build path (project's Properties window). When I did it the class could compile and run again.

Hopper answered 13/5, 2011 at 9:45 Comment(0)
A
0

Make sure that the .class files exist, usually below the bin directory.

In particular, if you have errors in unrelated files in the same project then the compilation may fail, and no .class files will be produced.

Accident answered 17/10, 2010 at 17:2 Comment(3)
No, I don't see .class files. I expected this, as compilation failed with "Could not find the main class" message.Kiaochow
The 'Could not find the main class' is not a compiler error. It's an error from Eclipse. If the compilation was working, there would be class files there. Try closing the project and/or cleaning. Have you got the Build Automatically checkedLakenyalaker
Yes, Build Automatically is checked. Cleaning or restarting doesn't solve the problem.Kiaochow
U
0

There can be the case of projects, containing errors, added to the build path of the application which prevents the completion of successful compilation. Make sure you remove any such project from the build path before running the application.

Removing these projects solved the problem for me.

Used answered 10/11, 2014 at 15:6 Comment(0)
V
0

Do you have a proper build tool setup? Like sbt have you installed it?

You can check its version by $sbt --version

If it is not setup you can download from here http://www.scala-sbt.org/download.html

You might have to restart your eclipse after installation.

Valorous answered 27/11, 2015 at 9:11 Comment(1)
If you're asking for additional information, it should be asked in comments.Irrespective
B
0

Just copy your XXX.scala file code. Remove the package and create a new Scala Class. Paste your XXX.scala code. (If you are using maven, do a maven clean and build.) Run configuration again. This works for me.

Bangs answered 17/10, 2016 at 19:27 Comment(0)
B
0

I have faced this issue. I have just deleted the package name, created scala class, Written the same code, Set Build to "Build Automatically". Finally, It works perfectly fine.

Bangs answered 31/10, 2016 at 2:14 Comment(0)
F
0

Check scala-ide.log

For me the issue was that there were errors on:

AppData\Local\Temp\sbt_10d322fb\xsbt\ClassName.scala:16: error: not found: value enteringPhase enteringPhase(currentRun.flattenPhase.next) { s fullName separator }

Flannelette answered 12/1, 2017 at 11:54 Comment(0)
F
0

For me it was Eclipse specific problem. I noticed that .class file wasn't built at all. So bin directory doesn't have compiled classes. When I manually compiled *.scala file using *.sbt and copied it to bin directory it was working as expected. I tried different tips and tricks and it wasn't worked until I reinstalled Scala plugin in Eclipse .

Fredrick answered 5/3, 2017 at 16:57 Comment(0)
P
0

If you are using Intellij, mark directory as source root

Paleoclimatology answered 13/6, 2017 at 20:59 Comment(1)
They stated in their question that they are using Eclipse.Cockleboat

© 2022 - 2024 — McMap. All rights reserved.