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 independency.so 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.
     
    #21
  2. rahulgakhar

    rahulgakhar Certified CEan

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

    Sada Star

    Engineering Discipline:
    Computer Science
    yes, we can. By using static block.
    Go through the previous posts of this thread.
     
    #23
  4. Amit Porwal

    Amit Porwal Certified CEan

    Engineering Discipline:
    IT
    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);


    Example............

    Code:
    class staticEx 
    {
            static 
            {
                    System.out.println("Inside Static Block");
                    System.exit(0);
             }
    }
    
    AMIT PORWAL, BANGALORE(soft. eng.)


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

    crazy_raja Certified CEan

    Engineering Discipline:
    Computer Science
    class Hello
    {
    static
    {
    System.out.println("This is the Java Program That Runs Without Main Method");
    System.exit(0);
    }
    }


    Try this one....it works
     
    #25
  6. Ricky008

    Ricky008 Certified CEan

    Engineering Discipline:
    Aerospace
    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" );
    System.exit(0);
    }
    }
    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.
     
    #26
  7. karan20m

    karan20m Apprentice

    Engineering Discipline:
    Computer Science
    if we dont write main() in the progrm then the compiler doesnt know where to start the program..
     
    #27
  8. gayathri anbu

    gayathri anbu Certified CEan

    Engineering Discipline:
    Computer Science
    i have tried this.. it is not executing successfully..
     
    #28
  9. gayathri anbu

    gayathri anbu Certified CEan

    Engineering Discipline:
    Computer Science
    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.
    [TABLE]
    [TR]
    [TD]class MainMethodNot
    {
    static
    {
    System.out.println("This java program have run without the run method");
    System.exit(0);

    }
    }[/TD]
    [/TR]
    [/TABLE]

     
    #29
  10. karthikswapna1

    karthikswapna1 Certified CEan

    Engineering Discipline:
    IT
    yes of course the bellow program is executing
     
    #30
  11. vinci

    vinci Apprentice

    Engineering Discipline:
    Computer Science
    in reality this program wont work .my compiler gives me an error saying that main method not defined
     
    #31
  12. Sada

    Sada Star

    Engineering Discipline:
    Computer Science
    will work, Vinci. Check once again.

    And see this explanation,
     
    #32
  13. jeklin110

    jeklin110 Certified CEan

    Engineering Discipline:
    IT
    [TABLE]
    [TR]
    [TD="class: votecell"][/TD]
    [TD="class: answercell"][/TD]
    [/TR]
    [/TABLE]
    Hi,
    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
     
    #33
  14. jeklin110

    jeklin110 Certified CEan

    Engineering Discipline:
    IT
    Hi
    Welcome to you good question asked by your friends i am finding answer for you..
    ---------
     
    #34
  15. balaji MCA

    balaji MCA Certified CEan

    Engineering Discipline:
    IT
    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
     
    #35
  16. csmentor

    csmentor Certified CEan

    Engineering Discipline:
    Computer Science
    Hi,
    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.
     
    #36
  17. greatcoder

    greatcoder Apprentice

    Engineering Discipline:
    Computer Science
    Sada
    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
    Regards
    GC
     
    #37
  18. priya.123

    priya.123 Certified CEan

    Engineering Discipline:
    IT
    -----------------------------------------------------------------------------------
    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
     
    #38
  19. Anoop Kumar

    Anoop Kumar Mentor

    Engineering Discipline:
    IT
    Static block trick, no more applicable for JDK 1.7+:sneaky:
     
    #39
  20. Sada

    Sada Star

    Engineering Discipline:
    Computer Science
    Is it so? :confused:
     
    #40

Share This Page