Main method not found even if I've declared it

后端 未结 7 1716
感动是毒
感动是毒 2020-11-27 07:31

I want to create a simple java class, with a main method, but when I compile my code, I get this error message :

Error: Main method not found in class

相关标签:
7条回答
  • 2020-11-27 07:51

    Right click on the class name (which you are trying to run)->Run As->Run Configurations->Under Main Class Tab

    Write your main class name and hit on Run.

    0 讨论(0)
  • 2020-11-27 07:54

    As said in my comments, looks like you've declared a String class among your own classes. To prove this, I've created a basic example:

    class String {
    }
    
    public class CarelessMain {
        public static void main(String[] args) {
            System.out.println("won't get printed");
        }
        public static void main(java.lang.String[] args) {
            System.out.println("worked");
        }
    }
    

    If you execute this code, it will print "worked" in the console. If you comment the second main method, the application will throw an error with this message (similar for your environment):

    Error: Main method not found in class edu.home.poc.component.CarelessMain, please define the main method as:

    public static void main(String[] args)
    
    0 讨论(0)
  • 2020-11-27 07:57

    If the answers above are not working for you: make sure "main" is not capitalized in your method definition.

    OK: public static void main(String[] args)

    ERROR: public static void Main(String[] args)

    Though the error message makes the required syntax for the main method clear, incorrect caps can be hard to spot. It took me ~30 minutes of troubleshooting to find this typo today. This was clearly not an issue for the OP, but is another easy/likely way to produce the same error message, and this answer may help other users.

    0 讨论(0)
  • 2020-11-27 07:58

    I had this issue just now. This error came up because the JRE file that i switched out didn't had the full library. After I corrected/added the right JRE system library the problem went away.

    0 讨论(0)
  • 2020-11-27 07:59

    This usually happens if ur complete project isnotconfigured correctly or one of your class in project has still some errors in such cases IDE will prompt stating the same that project contains some error and you still proceed (ie run your class) as project has some bugs new classes will not be created and IDE will run the class which was available previously

    to make sure this is ur case u can add new class in your project and try to run it and if ur getting no such class exist then there its is a perfect evidence

    0 讨论(0)
  • 2020-11-27 07:59

    Just check your java file, it has not been saved. Please save all java files before compiling.

    0 讨论(0)
提交回复
热议问题