Hunt for India's Best Academic Project Contest - 2015

Important Links To Visit: 


Click: List of Shortlisted Participants | Click: Round II Details (Important) 


Can Java program run without the Main method?

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

  1. ashie

    ashie Apprentice

    yes sada ...we can run java program without using static block
    if a static block is present in the program,then jvm execute it first,and then search for main method...
    though if main method is not gave an that we can eliminate by using system.exit inside block method

    KACHA PIYUSH Certified CEan

    // SAVE IT Demo1 and this is successfully run my frd
    public class Demo1
    System.out.println("yeah sure painkiller");

  3. suraz

    suraz Certified CEan

    Program without main() used to run earlier,as static block is executed first and within static block you write sop and then terminate using system.exit(0).
    New version of java doesnot allow executing program without main()

    in jdk 1.7(build1.7.0-ea-b85),It gives run time Exception.Check it out
    • Like Like x 2
  4. Sada

    Sada Star

    yeah, so is there no chance to run a program in jdk1.7 without main()?
  5. Ankit Niranjan

    Ankit Niranjan Certified CEan

    but hw we can run this program??????
  6. Sada

    Sada Star

    please go through the above posts. You will find the explanation.
  7. Extreme Java

    Extreme Java Certified CEan

    Yes System.exit() is the right thing to do at the end of static block.
  8. Rashmi Yadav

    Rashmi Yadav Apprentice

    In java program ,the program execution starts from main method.But in java program all code must be declared in class. to access class we have to create an object for this.But to start execution all of this we have to execute main method .so have declared class as public static in which main method is saved.and we declare this class as file name to access direct main method without creating object of class in which the main method is saved.
    • Like Like x 1
  9. Great information friends. Thank you all.
    My question is that is there any specific problem field(s) to skip the main function and keep the program running using static block as stated above; because as per my view, we cannot have much of the versatility and complex functionality with the ystatic block as with the main function.
    So is there any special field of using static block like this?
  10. Rashmi Yadav

    Rashmi Yadav Apprentice

    If you want to execute a program you must include main method.if main method is not included in the program then the program will not execute and javac will give error.and there is nothing to replace the main have to include main method becoz main method is the reason of execution .you cant skip it with any other thing.
  11. madhuri thalluri

    madhuri thalluri Apprentice

    hello friends..
    I tried the above code in its showing error as follows
    so plz hlp me out..

    Attached Files:

  12. Anoop Kumar

    Anoop Kumar Mentor

    Check the following reply in this thread..

Share This Page