The “Main Method Not Found” error is one of the most common errors encountered by Java beginners. It occurs when the Java Virtual Machine (JVM) cannot locate the main() method, which serves as the entry point of a Java application.
Unlike compile-time errors such as Cannot Find Symbol, the Main Method Not Found error typically occurs at runtime when you attempt to execute a Java class that does not contain a valid main() method.
In this comprehensive guide, you’ll learn what the Main Method Not Found error is, why it occurs, common causes, practical examples, and the best ways to fix and prevent it.
What is the Main Method in Java?
The main() method is the entry point of every standalone Java application.
When you execute:
java Main
the JVM searches for the following method:
public static void main(String[] args)
If this method is missing or incorrectly declared, Java cannot start the program.
Correct syntax:
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Output:
What Does “Main Method Not Found” Mean?
When the JVM cannot locate a valid main() method, it displays an error similar to:
Error: Main method not found in class Main,
please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
This means:
- The class exists.
- The program compiled successfully.
- But the JVM cannot find the required entry point.
Why Does This Error Occur?
Java requires every executable application to contain exactly one properly declared main() method.
The required signature is:
public static void main(String[] args)
If any part of this declaration is incorrect, the JVM reports the error.
Common Causes of Main Method Not Found
Some of the most common reasons include:
- Missing main() method
- Incorrect method name
- Wrong return type
- Missing static keyword
- Missing public keyword
- Incorrect parameter list
- Wrong capitalization
- Running the wrong class
- Method declared inside another method
- Executing a library class instead of the application class
Example 1: Missing Main Method
Incorrect code:
public class Main {
public void display() {
System.out.println("Hello");
}
}
Output:
Correct code:
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Example 2: Incorrect Method Name
Incorrect:
public static void Main(String[] args) {
System.out.println("Hello");
}
Output:
Java is case-sensitive.
Correct:
public static void main(String[] args) {
System.out.println("Hello");
}
The method name must be exactly main.
Example 3: Missing static Keyword
Incorrect:
public void main(String[] args) {
System.out.println("Hello");
}
Output:
Correct:
public static void main(String[] args) {
System.out.println("Hello");
}
The JVM calls the main() method without creating an object, so it must be static.
Example 4: Missing public Keyword
Incorrect:
static void main(String[] args) {
System.out.println("Hello");
}
Output:
Correct:
public static void main(String[] args) {
System.out.println("Hello");
}
The JVM requires the method to be publicly accessible.
Example 5: Wrong Return Type
Incorrect:
public static int main(String[] args) {
return 0;
}
Output:
Correct:
public static void main(String[] args) {
}
The return type must always be void.
Example 6: Incorrect Parameter Type
Incorrect:
public static void main(String args) {
}
ct:
Correct:
public static void main(String[] args) {
}
The parameter must be a String array.
You can also write:
public static void main(String... args) {
}
Both forms are valid.
Example 7: Wrong Class Execution
Suppose your project contains:
Main.java
Student.java
Employee.java
Only Main.java contains:
public static void main(String[] args)
If you execute:
java Student
Java reports:
Main method not found in class Student
Run the correct class instead:
java Main
Example 8: Main Method Outside the Class
Incorrect:
public class Main {
}
public static void main(String[] args) {
}
This does not compile because the main() method must be declared inside a class.
Correct:
public class Main {
public static void main(String[] args) {
}
}
Example 9: Incorrect Class Name
Suppose your file is:
Main.java
But you execute:
java Demo
Output:
Always execute the class that contains the main() method.
Example 10: Using an IDE with the Wrong Run Configuration
Sometimes the code is correct, but your IDE is configured to run the wrong class.
Solutions:
- Verify the Run Configuration.
- Select the correct main class.
- Rebuild the project.
- Restart the IDE if necessary.
Example 11: Running a Library Class
Many utility classes are not executable.
Example:
public class MathUtility {
public static int add(int a, int b) {
return a + b;
}
}
Running:
java MathUtility
produces:
Main method not found
Utility classes are intended to be used by other classes, not executed directly.
Example 12: Nested Main Method Mistake
Incorrect:
public class Main {
public void display() {
public static void main(String[] args) {
}
}
}
Methods cannot be declared inside other methods.
Place main() directly inside the class.
How to Fix the Main Method Not Found Error
Follow these troubleshooting steps:
1. Verify the Method Signature
Ensure your method is exactly:
public static void main(String[] args)
2. Check the Method Name
The name must be:
main
Not:
Main
MAIN
mian
3. Ensure the Method is Public
Always declare:
public
4. Ensure the Method is Static
Without static, the JVM cannot invoke the method.
5. Use the Correct Return Type
Always use:
void
6. Verify the Parameter
Valid forms:
String[] args
or
String... args
7. Run the Correct Class
Ensure you’re executing the class that actually contains the main() method.
8. Clean and Rebuild the Project
If using Maven:
mvn clean install
If using Gradle:
gradle clean build
Rebuilding removes stale class files that may cause execution issues.
Real-World Example
Incorrect implementation:
public class Student {
public void display() {
System.out.println("Student");
}
}
Command:
java Student
Output:
Correct implementation:
public class Student {
public static void main(String[] args) {
System.out.println("Student");
}
}
Output:
Common Scenarios That Cause Main Method Not Found
| Scenario | Solution |
|---|---|
| Missing main() method | Add the required entry point |
| Incorrect method name | Use main exactly |
| Missing static | Declare the method as static |
| Missing public | Declare the method as public |
| Wrong return type | Use void |
| Incorrect parameter | Use String[] args or String… args |
| Running the wrong class | Execute the class containing main() |
| Wrong IDE run configuration | Select the correct main class |
| Utility class execution | Run only executable classes |
| Incorrect method placement | Declare main() directly inside the class |
Best Practices to Prevent Main Method Not Found
Follow these recommendations:
- Always use the standard main() method signature.
- Let your IDE generate the main() method automatically.
- Avoid changing the method name or parameter list.
- Ensure the method is public, static, and returns void.
- Keep only one clear application entry point.
- Verify your IDE’s run configuration before execution.
- Use meaningful class names for executable classes.
- Clean and rebuild your project after major changes.
- Follow Java naming conventions.
- Test the application after refactoring.
Frequently Asked Questions (FAQs)
What is the main() method in Java?
The main() method is the entry point of a standalone Java application. The JVM begins executing the program from this method.
Why does Java say “Main Method Not Found”?
This error occurs because the JVM cannot locate a valid public static void main(String[] args) method in the class you are trying to run.
Can I have multiple main() methods?
Yes. Different classes in the same project can each have their own main() method. However, when you run the application, you must specify which class should be executed.
How do I fix the Main Method Not Found error?
You can resolve it by:
- Declaring the correct main() method signature.
- Using the exact method name main.
- Adding the public and static keywords.
- Using void as the return type.
- Passing String[] args or String… args as the parameter.
- Running the correct class.
- Checking your IDE’s run configuration.
Conclusion
The Main Method Not Found error is one of the most common runtime issues faced by Java developers, especially beginners. It occurs when the JVM cannot locate a valid application entry point due to an incorrect method signature, missing keywords, or running the wrong class. Fortunately, the solution is straightforward: ensure your class contains the exact public static void main(String[] args) method, verify that you are executing the correct class, and check your project configuration. By following Java’s standard conventions and using your IDE’s code-generation features, you can easily avoid this error and build Java applications that run successfully from the first execution.