The java error: main method not found in the file, please define the main method as: public static void main(string[] args) occurs if the main method not found in class or the main method is not accessible or the main method is invalid. The main method indicates the start of the application. The main method syntax must be as public static void main(String[] args) { }. Alternatively, java program must extend javafx.application.Application. Otherwise, “or a JavaFX application class must extend javafx.application.Application” error will be shown.
In Java, each application must have the main method. The main method is the entry point of every java program. The java program searches for the main method while running. This error occurred when the main method is not available.
In java, the JavaFX application does not to have a main method, instead the JavaFX application must extend javafx.application.Application class. Otherwise “Error: Main method not found in class, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application” exception will be thrown in java.
Exception
Error: Main method not found in class, 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, please define the main method as:
public static void main(String[] args)
Error: Main method must return a value of type void in class, please
define the main method as:
public static void main(String[] args)
Error Code
The Error code looks like as below. If you run javac Test.java & java Test The above exception will be thrown. The Test class neither have main method nor extend javafx.application.Application class.
package com.yawintutor;
public class Test {
}
Root Cause
Spring boot is looking for the main method to start the spring boot application. This error occurred because the main method is not available.
Any one of the following is the possible cause of this issue
- The main method is deleted
- The main method name is renamed
- The signature of the main method is changed
public static void main(String[] args) {
// block of code
}
Solution 1
The java main method is the entry point of the java program. If the main method is missing in the program, the exception will be thrown. Check the main method in the code, and add the main method as shown below.
package com.yawintutor;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
The below example shows the main method of the spring boot application.
package com.yawintutor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringHelloWorldApplication {
public void main(String[] args) {
SpringApplication.run(SpringHelloWorldApplication.class, args);
}
}
Solution 2
The main method in the java application must be public. If it is not a public, java compiler can not access the main method. If the access specifier is not specified or not a public, then the exception will be thrown.
package com.yawintutor;
public class Test {
static void main(String[] args) {
System.out.println("Hello World");
}
}
Output
Error: Main method not found in class com.yawintutor.Test, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Solution
package com.yawintutor;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Solution 3
If the main method does not have static keyword, the main method can not access from outside of the class. The object of the class only can access the main method. To create a object, the main method must be executed. This will cause the exception.
package com.yawintutor;
public class Test {
public void main(String[] args) {
System.out.println("Hello World");
}
}
Output
Error: Main method is not static in class com.yawintutor.Test, please define the main method as:
public static void main(String[] args)
Solution
package com.yawintutor;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Solution 4
The main method must return void. If the return data type is added other than void. the exception will be thrown.
package com.yawintutor;
public class Test {
public static int main(String[] args) {
System.out.println("Hello World");
return 0;
}
}
Output
Error: Main method must return a value of type void in class com.yawintutor.Test, please
define the main method as:
public static void main(String[] args)
Solution
package com.yawintutor;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Solution 5
The main method must have only one argument. The argument must be a string array. If no argument is configured or different data type argument is configured or more than one argument is configured, the exception will be thrown.
package com.yawintutor;
public class Test {
public static void main() {
System.out.println("Hello World");
}
}
Output
Error: Main method not found in class com.yawintutor.Test, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Solution
package com.yawintutor;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World");
}
}