92

Just going through the sample Scala code on Scala website, but encountered an annoying error when trying to run it.

Here's the code: http://www.scala-lang.org/node/45. On running it on Eclipse, I got this message 'Editor does not contain a main type' that prevents it from running.

Is there anything I need to do...i.e break that file into multiple files, or what?

Helen Neely
  • 4,666
  • 8
  • 40
  • 64

35 Answers35

90

In Eclipse, make sure you add your source folder in the project properties -> java build path -> source. Otherwise, the main() function may not be included in your project.

milkplus
  • 33,007
  • 7
  • 30
  • 31
38

I have this problem a lot with Eclipse and Scala. It helps if you clean your workspace and rebuild your Project.

Sometimes Eclipse doesn't recognize correctly which files it has to recompile :(

Edit: The Code runs fine in Eclipse

OpiesDad
  • 3,385
  • 2
  • 16
  • 31
nuriaion
  • 2,621
  • 2
  • 23
  • 18
  • 4
    Made the same experience. Plus closing and opening the project seemed to be required for me sometimes. – Fabian Steeg Jul 11 '09 at 15:09
  • Thanks @nuriaion and @Fabian, yes, reopening the project got it working. Thanks again for your tips. – Helen Neely Jul 11 '09 at 15:22
  • 4
    @Helen In Eclipse => Project => Clean you can clean the projects, the rebuild will run automaticaly... So you don't have to close and reopen the project. – nuriaion Jul 12 '09 at 00:05
  • opening and closing the scala project gave me: Errors occurred during the build. Error instantiating builder 'ch.epfl.lamp.sdt.core.scalabuilder'. Plug-in "ch.epfl.lamp.sdt.core" was unable to instantiate class "scala.tools.eclipse.Builder". Could not initialize class scala.tools.eclipse.contribution.weaving.jdt.builderoptions.ScalaJavaBuilder Plug-in "ch.epfl.lamp.sdt.core" was unable to instantiate class "scala.tools.eclipse.Builder". Could not initialize class scala.tools.eclipse.contribution.weaving.jdt.builderoptions.ScalaJavaBuilder – Ustaman Sangat Jan 06 '12 at 20:03
31

A simpler way is to close the project and reopen it.

z3p5e6
  • 7
  • 2
static
  • 311
  • 3
  • 2
28

You have to make sure that your .java files are in the .src folder in eclipse. I had the same exact problem until I got it figured out.

ultra99
  • 413
  • 1
  • 7
  • 11
23

Just make sure that the folder you work in is added to the built path:

right-click your folder --> build Path --> Use as source Folder

and it should now find main therein.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
kholofelo Maloma
  • 973
  • 3
  • 15
  • 26
8

You can try to run the main function from the outline side bar of eclipse.

solution

Community
  • 1
  • 1
Fei
  • 591
  • 6
  • 7
  • When I do this, I got an error message: ` [in Tut_RMI] does not exit`. (The project name is `Tut_RMI`) Is this project name invalid? – Casper Feb 26 '17 at 10:53
7

I had the same problem. I tried all sorts of things. And I came to know that

  1. My .java files were not linked and
  2. they were not placed in the 'src' folder.

Things I did:

Project properties >> Java Build Path >> Source

  • Deleted the original 'src' folder which was empty using 'Remove' option
  • Added the source that contained my source .java files using the 'Add Folder' option

This solved the error.

Torsten
  • 1,696
  • 2
  • 21
  • 42
Anand Dhole
  • 83
  • 1
  • 7
6

Just close and reopen your project in Eclipse. Sometime there are linkage problems. This solved my problem

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Mohit Singh
  • 5,977
  • 2
  • 24
  • 25
4

What you should do is, create a Java Project, but make sure you put this file in the package file of that project, otherwise you'll encounter same error.

enter image description here

shanethehat
  • 15,460
  • 11
  • 57
  • 87
4

A quick solution:

First, exclude the package: Right click on the source package >> Build Path >> Exclude

Then include it back: Right click on the source package >> Build Path >> Include

rwc
  • 41
  • 1
3

That code is valid. Have you tried to compile it by hand using scalac? Also, have you called your file "addressbook", all lowercase, like the name of the object?

Also, I found that Eclipse, for some reason, set the main class to be ".addressbook" instead of "addressbook".

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • Thanks Daniel, yes the file is named 'addressbook' all lowercase. Will try reopening the project to see if that helps. – Helen Neely Jul 11 '09 at 15:16
3

Make sure that your .java file is present either in the str package, or in some other package. If the java file with the main function is outside all packages, this error is thrown.

3

Have faced the similar issue, resolved this by right clicking on the main method in the outline view and run as Java application.

Naresh
  • 31
  • 1
3

you should create your file by

selecting on right side you will find your file name,

under that will find src folder their you right click select -->class option

their your file should be created

agf
  • 171,228
  • 44
  • 289
  • 238
vijaya
  • 31
  • 1
2

I just had this problem too. The solution is to make sure eclipse created the project as Java project. Just create a new Java project and copy your class into the src folder (and import the eventual dependencies). This should fix the problem.

KullDox
  • 353
  • 1
  • 3
  • 11
  • Thanks KullDox, for your comment. I've long solved the issue but hope someone else finds the solutions here helpful. – Helen Neely Apr 15 '10 at 11:58
2

The correct answer is: the Scala library needs to before the JRE library in the buildpath.

Go to Java Buildpath > Order and Export and move Scala library to the top

MGM
  • 21
  • 1
1

I had this problem with a Java project that I imported from the file system (under Eclipse Helios). Here's a hint: the src code didn't seem to be compiled at all, as no "bin" directory showed up.

I had to create a Java project from scratch (using the wizard), then compare the .project files of the non-working and working projects.

The project giving "Editor does not contain a main type" had this as the "buildSpec" in the .project file:

<buildSpec>
</buildSpec>

But the working project had this as the "buildSpec":

<buildSpec>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>

I copied this in, and the imported project worked.

I know my answer is for Java, but the same might be the issue for your Scala project.

1

May be the file you have created is outside the src(source) folder. Trying to call the class object(from the file located in the src folder) from the .java file outside the source folder results in the same error. Copy .java file to the source folder, then build it. The error will be gone.

Vamsi Tallapudi
  • 3,633
  • 1
  • 14
  • 23
1

I had the same problem. I had the main class out of the src package, in other folder. I move it in and correct folder and solved

jacruz
  • 56
  • 4
1

run "eclipse -clean -refresh" from command line. This fixed the issue for me when all other solutions failed.

MikeG
  • 11
  • 1
1

This could be the issue with the Java Build path. Try below steps :

  1. Go to project properties
  2. Go to java Build Path
  3. Go to Source tab and add project's src folder

This should resolve the issue.

AalekhG
  • 361
  • 3
  • 8
1

If it is maven project please check the java file is created under src/main/java

If you are not getting please change the JRE path and create the java files in above folder structure

ssomu
  • 79
  • 7
0

In the worst case - create the project once again with all the imports from the beginning. In my case none of the other options worked. This type of error hints that there is an error in the project settings. I once managed to solve it, but once further developments were done, the error came back. Recreating everything from the beginning helped me understand and optimize some links, and now I am confident it works correctly.

Sergiu
  • 349
  • 4
  • 8
0

File >> Import >> Existing Projects into Workspace >> Select Archive Filed >> Browse and locate file >> Finish. If its already imported some other way delete it and try it that way. I was having the same problem until i tried that.

Mike
  • 1
0

One more thing to check: make sure that your source file contains the correct package declaration corresponding to the subdirectory it's in. The error mentioned by the OP can be seen when trying to run a "main type" declared in a file in a subdirectory but missing the package statement.

Daniel Ashton
  • 1,388
  • 11
  • 23
0

I have this problem too after I changed the source folder. The solution that worked for is just editing the file and save it.

Hunsu
  • 3,281
  • 7
  • 29
  • 64
0

Try 'Update Project'. Once I did this, The Run as Java Application option appeared.

George Santhosh
  • 119
  • 2
  • 13
0

In my particular 'Hello World' case the cause for this problem was the fact, that my main() method was inside the Scala class.

I put the main() method under the Scala object and the error disappeared.

That is because Scala object in Java terms is the entity with only static members and methods inside.

That is why Java's public static void main() in Scala must be placed under object.

(Scala class may not contain static's inside)

Vlad.Bachurin
  • 1,340
  • 1
  • 14
  • 22
0

On reason for an Error of: "Editor does not contain a main type"

Error encountered in: Eclipse Neon

Operating System: Windows 10 Pro

When you copy your source folders over from a thumb-drive and leave out the Eclipse_Projects.metadata folder.

Other than a fresh install, you will have to make sure you merge the files from (Thrumb-drive)F:Eclipse_Projects.metadata.plugins .

These plug-ins are the bits and pieces of library code taken from the SDK when a class is created. I really all depends on what you-----import javax.swing.*;----- into your file. Because your transferring it over make sure to merge the ------Eclipse_Projects.metadata.plugins------ manually with a simple copy and paste, while accepting the skip feature for already present plugins in your Folder.

For windows 10: you can find your working folders following a similar pattern of file hierarchy.

C:Users>Mikes Laptop> workspace > .metadata > .plugins <---merge plugins here

0

For me, in Eclipse 3.6, this problem occurs when my main method is not public. I caused the problem by having a main method like this:

static void main(String[] args) 

The dubugger was unable to detect this by itself. I am pretty suprised Eclipse overlooked this.

djangofan
  • 28,471
  • 61
  • 196
  • 289
0

This is an easy way to solve it:

  1. Right click on source folder -> Build Path -> Remove from Build Path enter image description here

  2. Right click on source folder -> Build Path -> use as source folder

enter image description here

Abdelhadi Lahlou
  • 495
  • 5
  • 13
0

Follow the below steps:

  1. Backup all your .java files to some other location
  2. delete entire java project
  3. Create new java project by right click on root & click new
  4. restore all the files to new location !!
Unni Kris
  • 3,081
  • 4
  • 35
  • 57
-1

Instead of adding File, add class. File->New->Class. That fixed my issue.

Dmitriy
  • 5,525
  • 12
  • 25
  • 38
-1

Add a main method method inside the class to overcome this problem. I was facing the same issue regarding this.Now Including the main method inside my code i resolved my problem.

-1

You need locate file .java in folder src (in Project Explorer of Eclipse) and then it run. I have just add a file .java into project and it isn't in folder src, so I have a same error. I put again it into src, then it was build.

agf
  • 171,228
  • 44
  • 289
  • 238