How to fix the “editor does not contain a main type” error

The problem statement “editor does not contain a main type” is usually encountered while coding on java. The error is encountered on the IDE Eclipse. Eclipse is an environment (IDE) used for writing programs in different languages. It mostly exists for writing programs in Java but can also be utilized for different languages such as c/c++ or even JavaScript with the help of extensions.

This article will provide guidance on why the “editor does not contain a main type” problem occurs while trying to run code in Eclipse. We will also find out what to do to fix these issues can be fixed.

Resolve issue statement “editor does not contain a main type”

There exist various reasons that can invoke the error. A few of the major reasons are elaborated on in this section.

Reason 1: Absence of the main method

The most obvious reason for this problem is that your code is missing the main function in the syntax of your existing class. This is the main function which is absent:

public static void main(String[]args)

Solution: Add the main method

The solution for the above-stated reason is quite straightforward. We simply need to implement the main function into our java class.

Below is an example of how we can implement the main function:

Reason 2: Build path

Another reason for this error revolves around the build path. When the build path is not set to the correct location where your java project exists, it can cause the “editor does not contain a main type” error. Usually, we want the build path to be set to the “src” folder. The “src” folder is the main source folder that is created alongside the project. It contains the program files and acts as the default build path.

Solution: Check the build path

The solution to the second reason is to fix the build path of your project. Follow these steps to fix your build path:

Step 1: Remove the build path

Expand your project file and right-click on the “src” folder. Here, you will find the “Build Path”, click on it and then navigate to the “Remove from Build Path”.

Step 2: Set the build path

After removing it, click on the “Build Path” again where you will find the option “Use as Source Folder”.

Reason 3: Check the build path of your Java package

Reason 3 is that the file that we want to run is not present in our src folder. The Java file we want to execute lies inside the “javatest” package and our build path is currently set to this package as can be seen in the following image. 

Solution: Add Java package to src folder

To add your Java package back to the “src” directory:

  • Right click on the package and ‘hover the mouse on the “Build path” option.
  • Click on the “Remove from Build Path

By doing so, your package will be moved to the “src” directory as shown below:

Conclusion

There are 3 major reasons that can cause the issue “editor does not contain a main type”. One is the missing main function which we learned to add to the java class. Another is the build path is not correctly set. This error can be fixed by refreshing the build path or checking the build path of your package file (where the java file is located). Here, you have learned multiple solutions to avoid the problem “editor does not contain a main type”.