Error: Main method not found in class MyClass, please define the main method as...
When you use the java
command to run a Java application from the command line, e.g.,
java some.AppName arg1 arg2 ...
the command loads the class that you nominated and then looks for the entry point method called main
. More specifically, it is looking for a method that is declared as follows:
package some;
public class AppName {
...
public static void main(final String[] args) {
// body of main method follows
...
}
}
The specific requirements for the entry point method are:
- The method must be in the nominated class.
- The name of the method must be "main" with exactly that capitalization1.
- The method must be
public
. - The method must be
static
2. - The method's return type must be
void
. - The method must have exactly one argument and argument's type must be
String[]
3.
(The argument may be declared using varargs
syntax; e.g. String... args
. See this question for more information. The String[]
argument is used to pass the arguments from the command line, and is required even if your application takes no command-line arguments.)
If anyone of the above requirements is not satisfied, the java
command will fail with some variant of the message:
Error: Main method not found in class MyClass, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Or, if you are running an extremely old version of Java:
java.lang.NoSuchMethodError: main
Exception in thread "main"
If you encounter this error, check that you have a main
method and that it satisfies all of the six requirements listed above.
1 - One really obscure variation of this is when one or more of the characters in "main" is NOT a LATIN-1 character … but a Unicode character that looks like the corresponding LATIN-1 character when displayed.
2 - Here is an explanation of why the method is required to be static.
3 - String
must be the standard java.lang.String
class and not to a custom class named String
that is hiding the standard class.
Error :: Main method not found in class
To start a java program you need the main method which not define in your code you can make it like this :
public class Test {
public void printPhoto(int width, int height, boolean inColor) {
System.out.println("Width = " + width + " cm");
System.out.println("Height = " + height + " cm");
if (inColor) {
System.out.println("Print is Full color.");
} else {
System.out.println("Print is black and white.");
}
// printPhoto(10, 20, false); // Avoid a Stack Overflow due the recursive call
}
//main class
public static void main(String[] args) {
Test tst = new Test();//create a new instance of your class
tst.printPhoto(0, 0, true);//call your method with some values
}
}
Main method not found even if I've declared it
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)
Related Topics
"Expected Begin_Object But Was String At Line 1 Column 1"
How to Run a Batch File from My Java Application
Do We Have Any Generic Function to Check If Page Has Completely Loaded in Selenium
What Is an Initialization Block
Org.Xml.Sax.Saxparseexception: Content Is Not Allowed in Prolog
Why Is There No Sortedlist in Java
Does Java Support Default Parameter Values
Is There a Goto Statement in Java
Making a Robust, Resizable Swing Chess Gui
Arrays.Aslist() Not Working as It Should
How Can an App Use Files Inside the Jar For Read and Write
How to Implement Rest Token-Based Authentication With Jax-Rs and Jersey
Overriding the Java Equals() Method - Not Working
Something Seems Wrong With the Layout, Jbutton Showing Unexpected Behaviour At Resize of the Window