Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Main method not found even if I've declared it

Tags:

java

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 errors.TestErrors, please define the main method as: public static void main(String[] args)

This is the source code :

package errors;

public class TestErrors {
    public static void main(String[] args){
        System.out.println("hello");
    }
}

Why I'm seeing this error, as you can notice I've alreader declared the main method !

like image 703
user2874861 Avatar asked Oct 12 '13 20:10

user2874861


1 Answers

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)
like image 104
Luiggi Mendoza Avatar answered Oct 04 '22 14:10

Luiggi Mendoza