Can Java program run without the Main method?

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

  1. ashie

    ashie Apprentice

    Engineering Discipline:
    Electrical & Electronics
    yes sada ...we can run java program without main...by 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 found..it gave an error..bt that we can eliminate by using system.exit inside block method
     
    #41
  2. KACHA PIYUSH

    KACHA PIYUSH Certified CEan

    Engineering Discipline:
    Computer Science
    // PAIN-KILLER SOLUTION OF ALL LOGIC PROGRAM
    // SAVE IT Demo1 and this is successfully run my frd
    public class Demo1
    {
    static
    {
    System.out.println("yeah sure painkiller");
    System.exit(0);

    }
    }
     
    #42
  3. suraz

    suraz Certified CEan

    Engineering Discipline:
    Computer Science
    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
    #43
  4. Sada

    Sada Star

    Engineering Discipline:
    Computer Science
    yeah, so is there no chance to run a program in jdk1.7 without main()?
     
    #44
  5. Ankit Niranjan

    Ankit Niranjan Certified CEan

    Engineering Discipline:
    Computer Science
    but hw we can run this program??????
     
    #45
  6. Sada

    Sada Star

    Engineering Discipline:
    Computer Science
    please go through the above posts. You will find the explanation.
     
    #46
  7. Extreme Java

    Extreme Java Certified CEan

    Engineering Discipline:
    Computer Science
    Yes System.exit() is the right thing to do at the end of static block.
     
    #47
  8. Rashmi Yadav

    Rashmi Yadav Apprentice

    Engineering Discipline:
    Computer Science
    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
    #48
  9. Deepika Bansal

    Engineering Discipline:
    Computer Science
    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?
     
    #49
  10. Rashmi Yadav

    Rashmi Yadav Apprentice

    Engineering Discipline:
    Computer Science
    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 method.you have to include main method becoz main method is the reason of execution .you cant skip it with any other thing.
     
    #50
  11. madhuri thalluri

    madhuri thalluri Apprentice

    Engineering Discipline:
    Computer Science
    hello friends..
    I tried the above code in ecllipse..bt its showing error as follows
    so plz hlp me out..
     

    Attached Files:

    #51
  12. Anoop Kumar

    Anoop Kumar Mentor

    Engineering Discipline:
    IT
    Check the following reply in this thread..
     
    #52

Share This Page