aspose file tools*
The moose likes Beginning Java and the fly likes Java Program Without main() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Program Without main() method" Watch "Java Program Without main() method" New topic
Author

Java Program Without main() method

Anil Pattnaik
Greenhorn

Joined: Jun 15, 2006
Posts: 8
Hello Everybody,

I have seen one java program which will print "Hello World" without using main mthod.Inside static block the "System.out.println( "Hello World" );" statement is kept.But I did not understand how a java program can run
without main() method and why is "System.exit(0);" is used?Plese anybody can explain clearly how the following code is executed.Thank you.

public class WithoutMain
{
static
{
System.out.println( "Hello World" );
System.exit(0);
}
}
Daniel Bryant
Ranch Hand

Joined: Jul 15, 2006
Posts: 54
Hi Anil,

Every Java application begins with execution of an main method (even applets and servlets - it's just that the main method is hidden in the container), and therefore the code you supplied won't run without a main method - in fact if you type ">java WithoutMain" you will get a "Missing main method" exception.

The example you may have been referring to is as follows:



If you compile and run this code you will see the output "Hello World", but you will notice there is no code in the main method - the output occurs because every time a class is loaded into memory by the JVM (and I don't mean an object of a class being instantiated, just that the JVM inspects the class definition) any static initialiser blocks will execute.

This is why we see the output above - The JVM loads the class into memory (because it has to in order to execute the main method), the static initialiser block runs (printing output), and then the main method would run (which in this example would do nothing), except that the "System.exit(0)" terminates the program. That's all System.exit(0) does - it immediately stops execution of the program returning the value in the brackets to the operating system (0 is considered normal termination in unix, and -1 or 1 abnormal condition, such as a fatal error the program cannot recover from)

I hope this helps.

Daniel
[ August 03, 2006: Message edited by: Daniel Bryant ]

SCJP 1.4,<br />SCJD (URLyBird 1.2.1, JDK 5.0)
Anil Pattnaik
Greenhorn

Joined: Jun 15, 2006
Posts: 8
Hai all...
This is Anil.I had asked one question about a java program which is run without main() method.I have received one answer,but he has written main() method in his explaination.But this code runs without main() method.I want to know how this is possible,please explain.....public class WithoutMain
{
static
{
System.out.println( "Hello World" );
System.exit(0);
}
}
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

> The example you may have been referring to is as follows:

The OP's example is perfectly valid and will output

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

This is one of the standard examples of how to run a Java program without having a main() method and it has been published many times by me and many others.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

James is a bit closer, but it's not true that the "no main" message will be printed: that's what the System.exit() is for!

Here's how it works: When you start "java WithoutMain," the Java launcher finds the class WithoutMain and loads it. As you may know, static blocks like the one in this class are executed during the loading and initialization of a class. So while the class is being loaded, the message is printed. Next, the static block calls System.exit(), so the program ends after this.

If the static block didn't call exit, then the launcher would have next used reflection on that class to find the main() method; when it didn't find the method, it would print an error message and halt.

OK?


[Jess in Action][AskingGoodQuestions]
Daniel Bryant
Ranch Hand

Joined: Jul 15, 2006
Posts: 54
Hi all,

Apologies - I stand corrected. I ran the example in NetBeans IDE, and of course, it wouldn't let me execute the class without a main method. Another example of an IDE hiding the interesting stuff

A very interesting way to run your Java programs, and something I've never seen before...

Daniel
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Not really a beginner topic but...

A Java program only has to have a public static main() method if you are planning to invoke the Java program via the "java" executable, provided as part of the JDK or JRE. There are other ways to invoke a Java program.

You can write your own replacement for the "java" executable. Your replacement program loads a JVM, loads a class or classes and runs some methods of your choice. You can run main() if you like, but you can run anything else, too. This program can be written in C or C++, probably also in a variety of other languages, but I don't know. It's not trivial, but most decent C/C++ programmers would manage it, no problem.

Look up JNI and Java Invocation Interface.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Program Without main() method