No Main class found in NetBeans
Categories:
Resolving 'No Main Class Found' Errors in NetBeans

A comprehensive guide to diagnosing and fixing the common 'No Main class found' error when running Java projects in NetBeans 7 and later versions.
The 'No Main class found' error is a common frustration for Java developers, especially when working with IDEs like NetBeans. This error typically occurs when NetBeans cannot identify the entry point for your application, preventing it from compiling or running your project successfully. This article will walk you through the common causes of this error and provide step-by-step solutions to get your Java project running smoothly in NetBeans.
Understanding the 'No Main Class Found' Error
In Java, every executable application requires a main
method, which serves as the starting point for the Java Virtual Machine (JVM). The main
method must have a specific signature: public static void main(String[] args)
. If NetBeans cannot locate a method with this exact signature within your project's compiled classes, or if it's configured incorrectly, it will report the 'No Main class found' error. This can happen due to various reasons, from simple typos to incorrect project configurations.
flowchart TD A[Start Project Run] --> B{NetBeans Checks Project Configuration} B --> C{Is Main Class Defined?} C -- No --> D[Error: No Main Class Found] C -- Yes --> E{Does Main Class Exist and Have Correct Signature?} E -- No --> D E -- Yes --> F[JVM Invokes Main Method] F --> G[Application Runs Successfully]
Flowchart illustrating the NetBeans main class detection process.
Common Causes and Solutions
Several factors can lead to the 'No Main class found' error. Identifying the root cause is crucial for applying the correct fix. Below are the most frequent scenarios and their respective solutions.
1. Verify Main Method Signature
Ensure your main
method has the exact signature: public static void main(String[] args)
. Any deviation (e.g., String args[]
, missing static
, incorrect return type) will prevent the JVM from recognizing it as the entry point. Double-check for typos.
2. Check Project Properties (Run Configuration)
In NetBeans, right-click your project, select 'Properties', then navigate to the 'Run' category. Ensure that the 'Main Class' field is correctly set to the fully qualified name of your class containing the main
method (e.g., com.mycompany.myproject.MainClass
). If it's empty or incorrect, browse to select the correct one.
3. Ensure Class is in a Source Package
Your main class file (.java
) must reside within a source package (e.g., src/main/java
for Maven/Gradle, or a 'Source Package' folder in a standard NetBeans project). If it's directly under 'Source Files' without a package declaration, NetBeans might struggle to locate it.
4. Rebuild the Project
Sometimes, NetBeans' internal cache or build artifacts can become corrupted. Right-click your project, select 'Clean and Build'. This forces a complete recompilation and often resolves transient issues.
5. Check for Multiple Main Classes
If your project has multiple classes with main
methods, NetBeans might get confused about which one to use as the primary entry point. Explicitly set the 'Main Class' in the project properties to avoid ambiguity.
6. Verify Dependencies and Build Path
If your main class depends on external libraries, ensure they are correctly added to your project's classpath. Incorrect dependencies can lead to compilation failures, which in turn can prevent the main class from being properly identified.
package com.mycompany.myproject;
public class MyMainClass {
/**
* The main method is the entry point of the application.
* It must have the exact signature: public static void main(String[] args)
* @param args Command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello from MyMainClass!");
}
}
Example of a correctly structured Java main class.
Advanced Troubleshooting and Best Practices
If the basic solutions don't resolve the issue, consider these advanced steps and best practices to prevent future occurrences.
pom.xml
or build.gradle
file often specifies the main class. Ensure this configuration aligns with your NetBeans project settings.For Maven/Gradle Projects
If you're using a build tool like Maven or Gradle, the main class is often defined in the project's build configuration file. NetBeans usually picks this up automatically, but inconsistencies can cause problems.
Maven Example (pom.xml
):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mycompany.myproject.MyMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Maven pom.xml
configuration for specifying the main class.
Ensure the <mainClass>
tag contains the correct fully qualified name. After modifying pom.xml
, right-click the project -> 'Maven' -> 'Clean' and then 'Build'.
NetBeans Cache Issues
Sometimes, NetBeans' internal cache can become corrupted. Closing NetBeans, deleting the user cache directory (usually located at C:\Users\YOUR_USERNAME\AppData\Local\NetBeans\Cache
on Windows or ~/.cache/netbeans
on Linux/macOS), and then restarting NetBeans can resolve stubborn issues. This forces NetBeans to re-scan and re-index all projects.