• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Program Without main() method

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Anil Pattnaik
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Daniel Bryant
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic