Is the Main method must needed in a Java program?
Is the main method needed to write a java program?
This is my code:
package example;
public class HelloWorld {
public He开发者_Go百科lloWorld() {
}
public String getHelloWorld() {
return "Hello From Java!";
}
}
It shows an error at compilation:
java.lang.NoSuchMethodError: main
Exception in thread "main"
The main method is not needed in java programs. As others have pointed out, web applications do not use the main method.
It is not even needed in standalone applications. Consider
class JavaAppWithoutMain
{
static
{
System . out . println ( "Hello World!" ) ;
}
}
I compiled it and ran and obtained the following result:
Hello World!
Exception in thread "main" java.lang.NoSuchMethodError: main
For standalone applications you must either have
- a main method or
- a static initializer.
Main is preferred.
The main
method is the default entry point for a programme. If you don't define one, and then try to execute the jar produced, this is what you'll see. If you're not trying to produce a programme that needs launching independently, you won't need it - for example, a jar referenced by other programmes, or a website.
Without a main
method you application will have no entry point. Yes, it is required for any executable program.
If you try to execute a Java class, the JVM will look for a main
method to invoke it. From the CHAPTER 12 Execution of the Java Language Specification:
A Java virtual machine starts up by loading a specified class and then invoking the method
main
in this specified class. Section §12.1 outlines the loading, linking, and initialization steps involved in executingmain
, as an introduction to the concepts in this chapter. Further sections specify the details of loading (§12.2), linking (§12.3), and initialization (§12.4).
Not all classes need a main
, only the one that serve as "entry point" for execution.
The reason why you're getting this error message is because you're attempting to run a class using java (java.exe on Windows) and it's expecting to find a main() method.
This method isn't required as such but it can form an entry point where an application is initiated. You can rewrite your class as follows to achieve the result you were seeking:
package example;
public class HelloWorld {
// Running a class using java invokes this method
public static void main(String[] args) {
HelloWorld hw = new HelloWorld();
System.out.println( hw.getHelloWorld() );
}
public HelloWorld() {
}
public String getHelloWorld() {
return "Hello From Java!";
}
}
Standalone applications require main, because it is entry-point. How will JVM know where to start program?
No, it is not needed for e.g. web applications. They do not use a main()
method, but if you are testing or running a stand-alone application, to know what output you are expecting, you might require a main()
method.
Your java application or program (not every single class) needs atleast one main method to run it. And the one you have got is not a compilation error but a run time error.
"When you save program with the name same as the class name which contain main() method, then at the time of execution the JVM will create a object of that class and with that object JVM will call the main() metod as object.main().
So if main() method is missing( static initializer is also missing ) then it will throw an exception."
For web application same explanation as above.
ref: Java Understanding Java main method on logic
精彩评论