Editor does not contain a main type in Eclipse – Eclipse

Photo of author
Written By M Ibrahim
azure-java-sdk eclipse

Quick Fix: To resolve the error "Editor does not contain a main type", ensure that your project is correctly configured as a Java project in Eclipse:

  1. Right-click on the project folder and select "Properties".
  2. Navigate to the "Java Build Path" page.
  3. Under the "Source" tab, add the folders containing your Java source code using the "Add Folder" button.
  4. Apply the changes and save the project.

The Problem:

In Eclipse, upon compiling a simple Java code, users are encountering an error stating "Editor does not contain a main type." This issue arises despite having a Java-built-in Eclipse version and a 64-bit Linux operating system. The Java file is saved in a project folder, but the compilation process fails to recognize the main method. Inquiries have been made to resolve this error effectively.

The Solutions:

Solution 1: Link the project folder as a source folder

To resolve the "Editor does not contain a main type" error in Eclipse, you need to make sure that the project folder is properly recognized as a source folder.

  1. Right-click on the project folder in the Eclipse project explorer.
  2. Select Properties from the context menu.
  3. In the Properties dialog box, choose the Java Build Path option.
  4. Click on the Sources tab at the top of the dialog box.
  5. Click on the Add Folder button on the right panel.
  6. Select the folder containing the Java source files and click OK.
  7. Click OK to save the changes and close the Properties dialog box.

Now, Eclipse will recognize the project folder as a source folder, and you should no longer encounter the "Editor does not contain a main type" error when compiling and running Java programs.

Solution 2: Delete and Rewrite Signature of Main Method

This solution involves deleting the method signature for your main() method and then rewriting it. Here’s a step-by-step guide:

  1. Open the Java file where you have declared the main() method.

  2. Locate the main() method declaration, which should look something like this:

    public static void main(String[] args) {
        // Method body
    }
    
  3. Delete the entire method signature, which includes the public, static, void, main, and parameter list (String[] args).

  4. Save your changes.

  5. Re-write the main() method signature exactly as it was before, ensuring that you include the correct modifiers, return type, and parameter list.

  6. Save your changes again.

  7. Try compiling and running your program.

This solution has worked for some users who encountered the "Editor does not contain a main type" error. It is worth a try if you are facing a similar issue.

Solution 3: Eclipse’s Run Configuration

To resolve the “Editor does not contain a main type” error, follow these steps:

  1. Right-click on your project in the Package Explorer.
  2. Select "Run As" from the context menu.
  3. In the "Run As" dialog box, select "Run Configurations…" from the drop-down menu.
  4. In the "Run Configurations" dialog box, select "Java Application" from the left pane.
  5. Double-click on the "Java Application" configuration to edit it.
  6. In the "Main Class" field, click on the "Search" button and select the main class of your program.
  7. Click on the "Run" button to run your program.

Solution 4: Eclipse Will Not Automatically Look Outside of Source Folder

Eclipse is a widely used Java IDE, and it requires a specific project structure to compile and run Java programs. In this case, the issue seems to be with the project structure, specifically the location of the Sample.java file.

Eclipse expects Java source code to be organized in packages, which are represented as directories within the project’s source folder. By default, the source folder is named src, and it should be located at the root of the project directory.

In the problem statement, it is mentioned that the Sample.java file was placed directly under the project folder (Sample), instead of being placed inside a package directory within the source folder. This is likely the reason why Eclipse is not able to find the main method and is giving the "Editor does not contain a main type" error.

Resolution:

To resolve this issue, the Sample.java file should be placed within a package directory inside the source folder. Here’s how you can do it:

  1. Open the Eclipse project explorer.

  2. Right-click on the project folder (Sample) and select New > Package.

  3. Enter a name for the package, for example, com.example.sample.

  4. After creating the package, move the Sample.java file into the newly created package directory.

  5. Once the Sample.java file is inside the package directory, Eclipse should be able to find the main method and compile and run the program successfully.

Remember, in Java, the main method should be placed inside a class within a package. Eclipse expects this structure to properly compile and run Java programs. By organizing the project structure according to these conventions, you should be able to resolve the "Editor does not contain a main type" error.

Solution 5: Check if the main method exists, restart Eclipse, and run the program

  1. Verify the Main Method:

    • Ensure that the Java program you are trying to compile and run contains a main method. The main method is the entry point of a Java program, and it must be present in order for the program to run.
  2. Restart Eclipse:

    • Try restarting Eclipse. Sometimes, a simple restart can resolve temporary glitches and errors that may cause the "Editor does not contain a main type" issue.
  3. Run the Program:

    • After restarting Eclipse, right-click on the Java file containing the main method.
    • Select "Run As" and then choose "Java Application."

This should execute the Java program and resolve the "Editor does not contain a main type" error. If the issue persists, check if the JDK (Java Development Kit) is properly installed and configured in Eclipse. Additionally, ensure that the Java compiler (javac) is accessible from the command line or via the Eclipse preferences.