When I create a new main.java file in the default package in my Eclipse project, it generates a main
method that looks like:
public static void main(String[] args)
{
}
This immediately raises a warning that says This method has a constructor name
. The suggested fix is to remove the void
:
public static main(String[] args)
{
}
Now rather than a warning, I get an error: Illegal modifier for the constructor in type main; only public, protected & private are permitted
. If I remove the static
, my code now looks like:
public main(String[] args)
{
}
This time, I still get an error, but a different one that says:
Error: Main method not found in class main, please define the main method as:
public static void main(String[] args)
Argggh! But that takes me right back to where I started. How do I define the main method so that I don't get any errors or warnings?
I'm using Eclipse Juno Service Release 2 and JavaSE-1.7. Please note, I'm very new to Java; I come from a C# background. Also, this is probably a duplicate question, but I can't find it.
Don't call your class main
, but Main
.
In general, stick to the Java coding standards: start class names with a capital (Main
instead of main
) and you won't run into these problems.