File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Must or must not have main... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Must or must not have main..." Watch "Must or must not have main..." New topic

Must or must not have main...

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Every book on Java contains a line similar to:
"Every Java application must contain a main method whose signature looks like this:
public static void main(String[] args)"
which, IMHO is wrong:
public class FooBar{
new FooBar();
public FooBar(){
System.out.println("See, no main mommy");
java FooBar<enter>
will simply print "See, no main mommy" on the screen and exit (No error msgs). No main method needed.
I have not been able to find anyting on the main method in the specs, so is this simply a 'misinterpretation' or 'misconception' by *all* writers of java books, or is there something on the subject in the specs and i�ve missed it.
Any ideas??
Just an aftertought, excerpt from :
"If you try to invoke the Java interpreter on a class that does not have a main method, the interpreter refuses to run your program and displays an error message similar to this:
In class NoMain: void main(String argv[]) is not defined"
[ March 21, 2002: Message edited by: sven veer ]
Another link on the subject:
"5.2 Virtual Machine Start-up
The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (�5.3.1). The Java virtual machine then links the initial class, initializes it, and invokes its public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java virtual machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods. "
[ March 21, 2002: Message edited by: sven veer ]
Sean MacLean
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
The reason this *appears* to work is that the class loader must first load the class into
memory before it can attempts to call the main method. This means that the static initializer is going to be called (which prints out the message)first. In this particular example, however, you call System.exit imediateley after printing the message and the jvm doesn't have a chance to look for the main method. So it appears to work. If you remove the System.exit call, you get the standard ClassDefNotFound error. In my opinion, this works exactly as expected.
Hope this helps.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
You�re right, I never tested it with FooBar extenging a JFrame. Th JFrame never shows up ;-)
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
Also, it is a matter of symantics. A java "application" MUST have an entry point to start with a signature of:
publis static void main(String[] args)
There's no requriement that a Java class has a main. BUT if your class does NOT have a main(), you cannot call it a "java application".
Think about an applet. An Applet is NOT a java application (unless you actually include a main()). It's a different kind of class, and has a different context and life cycle. An applet MUST extend JApplet or Applet. If it doesn't then it's not an applet.
But as above, java classes do NOT all have to extend applet. BUT if you want to call it an applet, by definition it must extend Applet.

SCJP 1.4
I agree. Here's the link:
subject: Must or must not have main...
It's not a secret anymore!