Hunt for India's Best Academic Project (Applications Open)

Mega project competition for all THIRD and FINAL year engineering students in India. Prizes worth Rs. 80,000+ are ready to be won!


Deadline For Round I: March 10, 2015.


Click: Read Contest Details || Click: To Submit Your Entry


Click: Check Short Listed Entries [Updated Daily Till March 15th]

Can Java program run without the Main method?

Discussion in 'Computer Science | IT | Networking' started by Sada, Nov 17, 2009.

  1. naveen029

    naveen029 Certified CEan

    as you know,java achieves platform independency. so it uses Java Virtual Machiene(JVM) for platform java developer also connect main() method to this jvm.
    so when we run a program JVM does not find any entry gate(i.e main() method) into the program.lets take example..suppose you have a house connecting 2 to three rooms but atleast u requires a entry gate. so it is also necessary to have a main() methos like main() (entry gate in to run your program)i.e from where JVM can start.
  2. rahulgakhar

    rahulgakhar Certified CEan

    but my friend told me we can run without main..............
    bt he doesnt know how...........
  3. Sada

    Sada Star

    yes, we can. By using static block.
    Go through the previous posts of this thread.
  4. Amit Porwal

    Amit Porwal Certified CEan

    Re: Do a Java program runs without the Main method?

    We can run the program without using the main function in java......
    and we can print any value any string....

    if we want to print anythings in java without using main....
    then we use the static block.....

    with the use of static block program compile successfully but occur a run time error...
    for avoiding these run time error we can use the System.exit(0);


    class staticEx 
                    System.out.println("Inside Static Block");
    AMIT PORWAL, BANGALORE(soft. eng.)

    Moderator Note:: We recommend all communications to happen on CE!!
    Please donot post email ids.
  5. crazy_raja

    crazy_raja Certified CEan

    class Hello
    System.out.println("This is the Java Program That Runs Without Main Method");

    Try this works
  6. Ricky008

    Ricky008 Certified CEan

    Yes, you can but do not count on it when you develop your application. In Java, a static initializer gets executed as soon as the class is loaded, even before the main method is called. Here is an example,

    public class WithoutMain {
    static {
    System.out.println( "Hello World" );
    The JVM finds and loads the class into memory when you run this code, the static initializer is executed during the loading and initialization of the class. System.exit(0) is called at the end of the static block to terminates the program. If not, then the JVM would have next used reflection on that class to find the main() method. If it does not find the main method, it throws an exception.
  7. karan20m

    karan20m Apprentice

    if we dont write main() in the progrm then the compiler doesnt know where to start the program..
  8. gayathri anbu

    gayathri anbu Certified CEan

    i have tried this.. it is not executing successfully..
  9. gayathri anbu

    gayathri anbu Certified CEan

    You can write a runnable program which does not have main method at all. During run time jvm will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.
    [TD]class MainMethodNot
    System.out.println("This java program have run without the run method");


  10. karthikswapna1

    karthikswapna1 Certified CEan

    yes of course the bellow program is executing
  11. vinci

    vinci Apprentice

    in reality this program wont work .my compiler gives me an error saying that main method not defined
  12. Sada

    Sada Star

    will work, Vinci. Check once again.

    And see this explanation,
  13. jeklin110

    jeklin110 Certified CEan

    [TD="class: votecell"][/TD]
    [TD="class: answercell"][/TD]
    This is just convention. In fact, even the name main(), and the arguments passed in are purely convention.
    When you run java.exe (or javaw.exe on Windows), what is really happening is a coupleo of Java Native Interface (JNI) calls. These calls load the DLL that is really the JVM (that's right - java.exe is NOT the JVM). JNI is the tool that we use when we have to bridge between the virtual machine world, and the world of C, C++, etc... The reverse is also true - it is not possible (at least to my knowledge) to actually get a JVM running without using JNI.
    Struts2 Jquery
  14. jeklin110

    jeklin110 Certified CEan

    Welcome to you good question asked by your friends i am finding answer for you..
  15. balaji MCA

    balaji MCA Certified CEan

    Yes We can run the java program with out main method using static class name alone is enough to run a program in java sum operation only needs main clause
  16. csmentor

    csmentor Certified CEan

    In order to run a JAVA program INDEPENDENTLY you need to have main() method. You can create java classes without main method but they can be used in another program but cannot be run independently.

    Hope this helps.
  17. greatcoder

    greatcoder Apprentice

    I will tell u very conceptual answer.
    1) Main method is a method just like any other method defined by us. Suppose you make a method to add two numbers: add()
    2) U know the name of the method and can call it like add() and the program flow goes to add() method.
    3) Just think what happens when we excute the Java Program.
    4) We invoke the JVM and pass the name of the class to run
    5) What will JVM do? It will create an instance of the class we passed to it and will call a method "main()". Thus There must be an entry point so that a program can begin executing and in Java such a method from which the execution will start is named as "main()" method. After all JVM must know the method which it should call to start program execution...

    Hope this helps
  18. priya.123

    priya.123 Certified CEan

    You can run java program without main method with the help of static block
    Because it executes as soon as class loaded.
    Also memory is initialized to static block at the time of class compiled
    But still we require main() method because static block is called automatically whereas we can call main() method as on our require meant also to take command line argument we need main() method
  19. Anoop Kumar

    Anoop Kumar Mentor

    Static block trick, no more applicable for JDK 1.7+:sneaky:
  20. Sada

    Sada Star

    Is it so? :confused:

Share This Page