JAVAWarningApril 15, 2026

Syntax Error

Method main() is already defined in public class Main, please rename one of them

What This Error Means

This error occurs when Java encounters a syntax error in the code, such as a missing or mismatched bracket, or a semicolon in the wrong place. In this case, the error is caused by a duplicate main method, which is the entry point of a Java application.

Why It Happens

This error typically happens when a developer tries to write multiple main methods in the same class. In Java, there can only be one main method per class. If a developer tries to create another main method, Java will throw a compile-time error. This is because the main method is the entry point of a Java application, and having multiple main methods would cause confusion about which one to run.

How to Fix It

  1. 1To fix this error, you need to rename one of the main methods to a different name. For example, you could rename it to main2 or start, depending on what makes sense for your application. You can also use a different name for the class and leave the main method as it is. Here's an example of how you can fix this error:
  2. 2// Before (broken code)
  3. 3public class Main {
  4. 4 public static void main(String[] args) {
  5. 5 // code here
  6. 6 }
  7. 7 public static void main2(String[] args) {
  8. 8 // code here
  9. 9 }
  10. 10}
  11. 11// After (fixed code)
  12. 12public class Main {
  13. 13 public static void main(String[] args) {
  14. 14 // code here
  15. 15 }
  16. 16}
  17. 17// or
  18. 18public class Main2 {
  19. 19 public static void main(String[] args) {
  20. 20 // code here
  21. 21 }
  22. 22}

Example Code Solution

❌ Before (problematic code)
Java
public class Main {
  public static void main(String[] args) {
    // code here
  }

  public static void main2(String[] args) {
    // code here
  }
}
✅ After (fixed code)
Java
public class Main {
  public static void main(String[] args) {
    // code here
  }
}

// or
public class Main2 {
  public static void main(String[] args) {
    // code here
  }
}

Fix for Method main() is already defined in public class Main, please rename one of them

Related JAVA Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error