Is the main method needed in a Java program?

Is the main method necessary for writing a java program?

This is my code:

package example; public class HelloWorld { public HelloWorld() { } public String getHelloWorld() { return "Hello From Java!"; } } 

It shows a compilation error:

 java.lang.NoSuchMethodError: main Exception in thread "main" 
+7
java
source share
9 answers

In java programs, the main method is not required. As others noted, web applications do not use the main method.

This is not even required in standalone applications. Consider

 class JavaAppWithoutMain { static { System . out . println ( "Hello World!" ) ; } } 

I compiled it and ran and got the following result:

 Hello World! Exception in thread "main" java.lang.NoSuchMethodError: main 

For standalone applications you must have

  • main method or
  • static initializer.

Basic is preferred.

+13
source share

The main method is the entry point for the program. If you do not define it, and then try to execute the created jar, this is what you will see. If you are not trying to create a program that should run independently, you won’t need it - for example, the bank that other programs link to, or a website.

+9
source share

Without the main method , you will not have an entry point . Yes, this is required for any executable program.

+5
source share

If you try to execute the Java class, the JVM will look for the main method to call it. From CHAPTER 12 Executing the Java Language Specification:

The Java virtual machine starts by loading the specified class and then calling the main method in that specified class. Section §12.1 describes the loading, linking, and initialization steps performed by main , as an introduction to the concepts in this chapter. The following sections describe the loading details (§12.2) , linking (§12.3) and initialization (§12.4) .

Not all classes need main , only one that serves as the entry point for execution.

+5
source share

No, this is not required for, for example, web applications. They do not use the main() method, but if you are testing or running a standalone application to find out what result you expect, you may need the main() method.

+2
source share

Standalone applications require basic because it is an entry point. How does the JVM know where to start the program?

+2
source share

The reason you get this error message is because you are trying to run the class using java (java.exe on Windows) and expect to find the main () method.

This method is not required as such, but it can create an entry point at which the application is initiated. You can rewrite your class as follows to achieve the result you were looking for:

 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!"; } } 
+2
source share

Your java application or program (not for each individual class) should have at least one of the main methods for running it. And the one you have is not a compilation error, but a runtime error.

0
source share

"When you save a program with the same name as the name of the class that contains the main () method, then at run time the JVM will create an object of this class, and with this object the JVM will call the main () method as object.main ().

Thus, if the main () method is absent (the static initializer is also absent), it throws an exception.

For a web application, the same explanation as above.

ref: Java Understanding the core Java method in logic

0
source share

All Articles