10

Is the main method needed to write a java program?

This is my code:

package example;

public class HelloWorld {

    public HelloWorld() {

    }

    public String getHelloWorld() {

        return "Hello From Java!";
    }
}

It shows an error at compilation:

java.lang.NoSuchMethodError: main
Exception in thread "main"
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Praveen
  • 86,996
  • 72
  • 173
  • 215
  • This looks like a runtime error, not a compilation error. – Bolo Jul 11 '10 at 12:20
  • 1
    This Community Wiki question lists the possible causes of this common problem: http://stackoverflow.com/questions/5407250/causes-of-java-lang-nosuchmethoderror-main-exception-in-thread-main – Stephen C Jun 28 '11 at 14:42

9 Answers9

17

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

  1. a main method or
  2. a static initializer.

Main is preferred.

emory
  • 10,259
  • 1
  • 28
  • 55
10

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.

David M
  • 68,374
  • 13
  • 151
  • 181
7

Without a main method you application will have no entry point. Yes, it is required for any executable program.

Andrew Hare
  • 320,708
  • 66
  • 621
  • 623
6

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 executing main, 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.

Pascal Thivent
  • 535,937
  • 127
  • 1,027
  • 1,106
  • "Not all classes need a main, only the one that serve as "entry point" for execution." +1 For mentioning, this is something beginners often get wrong. – helpermethod May 24 '10 at 11:52
2

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
GustyWind
  • 2,920
  • 3
  • 39
  • 49
2

Standalone applications require main, because it is entry-point. How will JVM know where to start program?

Andrey
  • 56,384
  • 10
  • 111
  • 154
2

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!";
 }
}
James P.
  • 17,929
  • 27
  • 89
  • 147
0

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.

0

"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

Community
  • 1
  • 1
shubham mishra
  • 950
  • 12
  • 18