Nice programing

"오류 : MyClass 클래스에서 Main 메서드를 찾을 수 없습니다. main 메서드를 다음과 같이 정의하십시오…"

nicepro 2020. 12. 25. 22:56
반응형

"오류 : MyClass 클래스에서 Main 메서드를 찾을 수 없습니다. main 메서드를 다음과 같이 정의하십시오…"


새로운 Java 프로그래머는 Java 프로그램을 실행하려고 할 때 이러한 메시지를 자주 접하게됩니다.


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 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"

이것이 의미하는 바는 무엇이며 원인은 무엇이며이를 해결하려면 어떻게해야합니까?


java명령 줄에서 Java 응용 프로그램을 실행 하기 위해 명령을 사용하는 경우 (예 :

java some.AppName arg1 arg2 ...

이 명령은 지정한 클래스를로드 한 다음라는 진입 점 메서드를 찾습니다 main. 보다 구체적으로 다음과 같이 선언 된 메서드를 찾고 있습니다.

package some;
public class AppName {
    ...
    public static void main(final String[] args) {
        // body of main method follows
        ...
    }
}

진입 점 방법에 대한 특정 요구 사항은 다음과 같습니다.

  1. 메서드는 지명 된 클래스에 있어야합니다.
  2. 메소드의 이름은 정확히 대문자 1을 사용 하여 "main"이어야합니다 .
  3. 메서드는 public.
  4. 메서드는 static 2 여야합니다 .
  5. 메서드의 반환 유형은 void.
  6. 메서드에는 정확히 하나의 인수가 있어야하며 인수 유형은 String[] 3 이어야합니다 .

(인수 varargs 구문을 사용하여 선언 될 수 있습니다 . 예 : String... args. 자세한 내용은 이 질문 을 참조하십시오. String[]인수는 명령 줄에서 인수를 전달하는 데 사용되며 응용 프로그램에서 명령 줄 인수를 사용하지 않는 경우에도 필요합니다.)

위의 요구 사항 중 하나라도 충족되지 않으면 java명령이 실패하고 다음과 같은 메시지가 표시됩니다.

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

또는 매우 오래된 Java 버전을 실행중인 경우 :

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

이 오류가 발생하면 main방법이 있고 위에 나열된 6 가지 요구 사항을 모두 충족 하는지 확인하십시오 .


1-이것의 한 가지 모호한 변형은 "main"에있는 하나 이상의 문자가 LATIN-1 문자가 아니라 표시 될 때 해당 LATIN-1 문자 처럼 보이는 유니 코드 문자 인 경우입니다.

2 - 여기에 방법이 정적하는 데 필요한 이유에 대한 설명입니다.

3- 숨김 이라는 이름의 사용자 지정 클래스가 아닌 String해당 java.lang.String클래스에 해당해야 String합니다.


문제는 public void main(String[] args)호출하려는 클래스에 메서드 가 없다는 것 입니다.

그것

  • 반드시 static
  • 정확히 하나의 문자열 배열 인수가 있어야합니다 (무엇이든 이름이 지정 될 수 있음).
  • main은 소문자로 표기해야합니다.

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


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:


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.


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


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.


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


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.

ReferenceURL : https://stackoverflow.com/questions/5407250/error-main-method-not-found-in-class-myclass-please-define-the-main-method-as

반응형