"Error: Main method not found in class MyClass, please define the main method as..."
Asked Answered
S

11

66

New Java programmers often encounter messages like the following when they attempt to run a Java program. (Different Java tools, IDEs and so on give a variety of diagnostics for this problem.)


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

Error: Main method not found in the file, please define the main method as: 
   public static void main(String[] args)

Error: Main method is not static in class MyClass, please define the main method as:
   public static void main(String[] args)

Error: Main method must return a value of type void in class MyClass, please
define the main method as:
   public static void main(String[] args)

java.lang.NoSuchMethodError: main
Exception in thread "main"

What does this mean, what can cause it, and what should one do to fix it?

Sarmentum answered 23/3, 2011 at 15:2 Comment(2)
Just guessing they're calling a function that doesn't exist, but the compiler is supposed to stop you if you were to do that.Prawn
@Prawn - This is a case that the compiler cannot deal with. See my answer for details.Sarmentum
S
46

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:

  1. The method must be in the nominated class.
  2. The name of the method must be "main" with exactly that capitalization1.
  3. The method must be public.
  4. The method must be static 2.
  5. The method's return type must be void.
  6. 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.

Sarmentum answered 23/3, 2011 at 15:2 Comment(0)
F
14

The problem is that you do not have a public void main(String[] args) method in the class you attempt to invoke.

It

  • must be static
  • must have exactly one String array argument (which may be named anything)
  • must be spelled m-a-i-n in lowercase.

Note, that you HAVE actually specified an existing class (otherwise the error would have been different), but that class lacks the main method.

Fid answered 23/3, 2011 at 15:2 Comment(0)
H
8

Other answers are doing a good job of summarizing the requirements of main. I want to gather references to where those requirements are documented.

The most authoritative source is the VM spec (second edition cited). As main is not a language feature, it is not considered in the Java Language Specification.

Another good resource is the documentation for the application launcher itself:

Homager answered 23/3, 2011 at 15:2 Comment(0)
P
5

If you are running the correct class and the main is properly defined, also check if you have a class called String defined in the same package. This definition of String class will be considered and since it doesn't confirm to main(java.lang.String[] args), you will get the same exception.

  • It's not a compile time error since compiler just assumes you are defining a custom main method.

Suggestion is to never hide library java classes in your package.

Phelia answered 23/3, 2011 at 15:2 Comment(0)
I
3

The name of the exception suggests that the program tried to call a method that doesn't exist. In this context, it sounds like the program does not have a main method, though it would help if you posted the code that caused the error and the context in which the code was run.

This might have happened if the user tried to run a .class file or a .jar file that has no main method - in Java, the main method is the entry point to begin executing the program.

Normally the compiler is supposed to prevent this from happening so if this does happen, it's usually because the name of the method being called is getting determined ar run-time, rather than compile-time.

To fix this problem, a new programmer must either add the midding method (assuming still that it's main that's missing) or change the method call to the name of a method that does exist.

Read more about the main method here: http://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html

Idalla answered 23/3, 2011 at 15:2 Comment(1)
The compiler cannot deal with this. It doesn't know if the user is going to use the class that it is compiling as an "entry point" for the program, and therefore whether an appropriate main method ought to exist.Sarmentum
I
2

Generally, it means the program you are trying to run does not have a "main" method. If you are going to execute a Java program, the class being executed must have a main method:

For example, in the file Foo.java

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

This program should compile and run no problem - if main was called something else, or was not static, it would generate the error you experienced.

Every executable program, regardless of language, needs an entry point, to tell the interpreter, operating system or machine where to start execution. In Java's case, this is the static method main, which is passed the parameter args[] containing the command line arguments. This method is equivalent to int main(int argc, char** argv) in C language.

Interconnect answered 23/3, 2011 at 15:2 Comment(0)
E
1

In my case "main" method which is the entry point of the programs must be located in the public class which is named as the java file.

For an example refer the below case. enter image description here

Here, I've placed my main method inside the HelloWorld class which is named same as the java file name. And I created an object of Main class and the code runs correctly.

But if I placed "main" method inside the Main class, as it is not the public class in the java file code gives me the error ,

"Main method not found in class HelloWorld, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application"

Therefore main method must be placed in the public class of the java file.

Escutcheon answered 23/3, 2011 at 15:2 Comment(2)
I think this is an IDE-related problem. If you put the main method in the package-private Main class and try to run from the command prompt using the java Main (with a suitable classpath), then it should work. What is going on is that the IDE is assuming that when you "run" the "HelloWorld.java" file, that it should look for the main class in HelloWorld. (Consider: what if there were multiple classes in the file with a main method. Which of those classes should it run??)Sarmentum
Also: Please do not upload images of code/data/errors.. Using an image makes it impossible for people to cut-and-paste your example into a file and try it themselves.Sarmentum
U
1

I feel the above answers miss a scenario where this error occurs even when your code has a main(). When you are using JNI that uses Reflection to invoke a method. During runtime if the method is not found, you will get a

java.lang.NoSuchMethodError: No virtual method

Upheaval answered 23/3, 2011 at 15:2 Comment(1)
That's not the error message that the Question is designed to explained. The Question is specifically about a missing "main" method. (And please don't tell me that it isn't ... 'cos I wrote it.) If you want to address a different scenario, please feel free to open a different Question and self-answer it.)Sarmentum
I
0

For those who encountered this problem in Kotlin Project.

You can try deleting .idea folder and running the project again - for me it solved the issue. (better close IntelliJ first)

Seems like sometimes IntelliJ gets all confused about the main method signature and expecting the java signature instead of the Kotlin one.

Indra answered 23/3, 2011 at 15:2 Comment(0)
K
0

If you are using VSCode:

enter image description here

Choose: Clean Workspace

enter image description here

Choose: Restart and delete

Keep coding :-)

Kedron answered 23/3, 2011 at 15:2 Comment(1)
Note that this approach (resyncing your IDE) won't work unless the application does have a properly written entry-point class.Sarmentum
U
-1

Few min back i was facing " main method not defined".Now its resolved.I tried all above thing but nothing was working.There was not compilation error in my java file. I followed below things

  • simply did maven update in all dependent project (alt+F5).

Now problem solved.Getting required result.

Unready answered 23/3, 2011 at 15:2 Comment(2)
So what was the actual cause then? This sounds to me like you didn't build the application properly. If you think about it, that could be an answer to just about every programming question asked on StackOverflow! But we don't generally mention it ...Sarmentum
i build it using maven but in eclipse it didnot do it.Unready

© 2022 - 2024 — McMap. All rights reserved.