CrazyEngineers
  • My friend asked me this question.!

    Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?

    Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..

    CAn anyone tell me the reason?
    And correct me If am wrong?
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • faizaan

    MemberNov 17, 2009

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

    My point of view is that,

    If we create an applet in java than it can run without having main method,but if we develop an application than it do requires main method.

    Reason why we require main method is because we write public static void main() i.e main method is a static method & so we can call it without creating object of that class . so when we compile (i.e javac filename.java) java compiler needs to use this main method without creating any object of that class.

    please correct me if i am wrong.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberNov 17, 2009

    I think main() method is must in java ...
    since java is pure object oriented language....and also main() method is an entry point...through which execution of program begins............
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberNov 17, 2009

    @pallavi: Faizan is right it is possible to have a program to start execution without entering into main first.

    This is because main is always the entry gate for the program. but if we are able to provide any other initiation point there is no need of main

    as in case of an applet in which there is no main method, it has init
    Are you sure? This action cannot be undone.
    Cancel
  • Sahithi Pallavi

    MemberNov 18, 2009

    Thanks a ton guys...!

    So, main method is must to run a java program...!
    Are you sure? This action cannot be undone.
    Cancel
  • Sahithi Pallavi

    MemberNov 19, 2009

    guys..! My friend told me that we can write and run a java program without using the main method.
    i.e By using static

    she gave this example program...!

    public void static( )
    public class
    {
    public void static( )
    {
    System.out.println( "Without main method" );
    System.exit(0);
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberNov 19, 2009

    sahithi pallavi
    guys..! My friend told me that we can write and run a java program without using the main method.
    i.e By using static

    she gave this example program...!

    public void static( )
    public class
    {
    public void static( )
    {
    System.out.println( "Without main method" );
    System.exit(0);
    }
    }
    😕😕😕😕
    have you execute it...is it running successfully
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberNov 20, 2009

    @Pallavi: try the code yourself as I am doubtful this program will run.

    And I told you that Java Applet programs don't generally have the main method.

    they have paint() and init() method.
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberNov 20, 2009

    The java program can run without main method with this static method
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberNov 20, 2009

    ms_cs
    The java program can run without main method with this static method
    But where is the function name in this program.

    With which name you will save the file?
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberNov 20, 2009

    Yeah i am totally confused with this...where is the name of function
    as static is the reserved keyword..how can you use it...
    if this is possible.
    then what would we say it,,,,it's feature or limitation??
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberNov 20, 2009

    If I am not wrong ms_cs mean to say by using 'static block' not 'static method' because for calling a static method, one need to have an entry point while 'static block' is automatically called whenever class is loaded in JVM. "Static blocks" declared in any Java class are always called before the main() method. You can also note that why static variables or methods are declared outside "main" method.

    Let's see an example to avoid any confusions
    STEP 1: Create a class like followng in any of the locations of your sytem. Let me keep it "I:\"

    /**
     *
     * @author sookie
     */
    public class ProgramWithoutMainMethod {
     static
    {
        System.out.println("Look at me ! I am running without Main method");      
        System.exit(0);
        
    }
    }
    
    STEP 2: Now open command prompt and go to"bin" directory of your installed JDK and enter following command. It compiles the program
    javac I:\ProgramWithoutMainMethod.java
    [​IMG]

    STEP 3: Now let's simply run the program.
    java -classpath I:\ ProgramWithoutMainMethod
    [​IMG]

    Now you all might be wondering that why do I need to add this additional line "System.exit(0); " right? See, as soon as my "static block" reaches its closing brace, it tries to search for main() method which as of now you all now is must for running a Java program so in order to stop my program from looking for main() method, I would simply call System.exit(0); and terminate the entire Java program. So I am not giving it a chance to look for any main() method.

    Hoping now you all might be cleared about the things discussing over here.

    PS: Before directly asking questions [specially related to Java] - A tip try to search web, it is having so much information that you will learn 100 other things with only 1 question. 😀
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberNov 20, 2009

    thanks sookie ........for clarifying this concept
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberNov 20, 2009

    sookie: you are right..nice explanation
    Are you sure? This action cannot be undone.
    Cancel
  • accoolaryan

    MemberNov 29, 2009

    yes we can run a java program without a main method .as main method is necessary but if we use a block named static ,every statement in it will be executed without main
    try it
    but it can give a n error so use System.exit(0) at the end of the file
    as
    public Nomain{
    static{
    System.out.println("printing without main");
    }
    System.exit(0);
    }
    Are you sure? This action cannot be undone.
    Cancel
  • rama_krish627

    MemberNov 29, 2009

    107627@ccc108:~> javac crazy1.java
    crazy1.java:1: 'class' or 'interface' expected
    public void static( )
    ^
    crazy1.java:2: <identifier> expected
    public class
    ^
    crazy1.java:9: '{' expected
    }
    ^
    3 errors

    These are the errors in this program.
    can you tell me is it working properly?
    If yes tell me how.
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberNov 30, 2009

    ya nice explanation sookie
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberNov 30, 2009

    rama_krish627
    107627@ccc108:~> javac crazy1.java
    crazy1.java:1: 'class' or 'interface' expected
    public void static( )
    ^
    crazy1.java:2: <identifier> expected
    public class
    ^
    crazy1.java:9: '{' expected
    }
    ^
    3 errors

    These are the errors in this program.
    can you tell me is it working properly?
    If yes tell me how.
    Pardon my ignorance but I couldn't see where is your program crazy1.java ?

    I am just assuming
    You are using "class" keyword in your program and name of the file and class marked as public are same.
    Are you sure? This action cannot be undone.
    Cancel
  • techno_ishant

    MemberNov 30, 2009

    java program can run without main()
    For Example APPLET...😁
    Are you sure? This action cannot be undone.
    Cancel
  • rahulgakhar

    MemberMay 7, 2010

    ya u can run a java program without main

    try this

    public class A
    {
    static
    {
    System.out.println( "Without main method" );
    System.exit(0);
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • naveen029

    MemberMay 12, 2010

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • rahulgakhar

    MemberMay 12, 2010

    but my friend told me we can run without main..............
    bt he doesnt know how...........
    Are you sure? This action cannot be undone.
    Cancel
  • Sahithi Pallavi

    MemberMay 13, 2010

    rahulgakhar
    but my friend told me we can run without main..............
    bt he doesnt know how...........
    yes, we can. By using static block.
    Go through the previous posts of this thread.
    Are you sure? This action cannot be undone.
    Cancel
  • Amit Porwal

    MemberOct 17, 2010

    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............

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • crazy_raja

    MemberAug 8, 2011

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


    Try this one....it works
    Are you sure? This action cannot be undone.
    Cancel
  • Ricky008

    MemberAug 8, 2011

    sahithi pallavi
    My friend asked me this question.!

    Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?

    Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..

    CAn anyone tell me the reason?
    And correct me If am wrong?
    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.
    Are you sure? This action cannot be undone.
    Cancel
  • karan20m

    MemberAug 9, 2011

    if we dont write main() in the progrm then the compiler doesnt know where to start the program..
    Are you sure? This action cannot be undone.
    Cancel
  • gayathri anbu

    MemberAug 9, 2011

    i have tried this.. it is not executing successfully..
    Are you sure? This action cannot be undone.
    Cancel
  • gayathri anbu

    MemberAug 9, 2011

    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]

    Are you sure? This action cannot be undone.
    Cancel
  • karthikswapna1

    MemberOct 20, 2011

    yes of course the bellow program is executing
    Sada
    guys..! My friend told me that we can write and run a java program without using the main method.
    i.e By using static

    she gave this example program...!

    public void static( )
    public class
    {
    public void static( )
    {
    System.out.println( "Without main method" );
    System.exit(0);
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • vinci

    MemberOct 21, 2011

    in reality this program wont work .my compiler gives me an error saying that main method not defined
    Are you sure? This action cannot be undone.
    Cancel
  • Sahithi Pallavi

    MemberOct 23, 2011

    vinci
    in reality this program wont work .my compiler gives me an error saying that main method not defined
    will work, Vinci. Check once again.

    And see this explanation,
    If I am not wrong ms_cs mean to say by using 'static block' not 'static method' because for calling a static method, one need to have an entry point while 'static block' is automatically called whenever class is loaded in JVM. "Static blocks" declared in any Java class are always called before the main() method. You can also note that why static variables or methods are declared outside "main" method.

    Let's see an example to avoid any confusions
    STEP 1: Create a class like followng in any of the locations of your sytem. Let me keep it "I:\"

    Code:

    /** * * @author sookie */ public class ProgramWithoutMainMethod { static { System.out.println("Look at me ! I am running without Main method"); System.exit(0); } }
    STEP 2: Now open command prompt and go to"bin" directory of your installed JDK and enter following command. It compiles the program
    javac I:\ProgramWithoutMainMethod.java
    [​IMG]

    STEP 3: Now let's simply run the program.
    java -classpath I:\ ProgramWithoutMainMethod
    [​IMG]

    Now you all might be wondering that why do I need to add this additional line "System.exit(0); " right? See, as soon as my "static block" reaches its closing brace, it tries to search for main() method which as of now you all now is must for running a Java program so in order to stop my program from looking for main() method, I would simply call System.exit(0); and terminate the entire Java program. So I am not giving it a chance to look for any main() method.

    Hoping now you all might be cleared about the things discussing over here.

    PS: Before directly asking questions [specially related to Java] - A tip try to search web, it is having so much information that you will learn 100 other things with only 1 question. 😀
    Are you sure? This action cannot be undone.
    Cancel
  • jeklin110

    MemberNov 2, 2011

    [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.
    ----------
    #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
  • jeklin110

    MemberNov 3, 2011

    Hi
    Welcome to you good question asked by your friends i am finding answer for you..
    ---------
    Are you sure? This action cannot be undone.
    Cancel
  • balaji MCA

    MemberFeb 29, 2012

    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
    Are you sure? This action cannot be undone.
    Cancel
  • csmentor

    MemberMar 15, 2012

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • greatcoder

    MemberMar 15, 2012

    #-Link-Snipped-#
    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
    Are you sure? This action cannot be undone.
    Cancel
  • priya.123

    MemberMay 18, 2012

    Sada
    My friend asked me this question.!

    Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?

    Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..

    CAn anyone tell me the reason?
    And correct me If am wrong?
    -----------------------------------------------------------------------------------
    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
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberMay 18, 2012

    Static block trick, no more applicable for JDK 1.7+😏
    Are you sure? This action cannot be undone.
    Cancel
  • Sahithi Pallavi

    MemberMay 18, 2012

    ianoop
    Static block trick, no more applicable for JDK 1.7+😏
    Is it so? 😕
    Are you sure? This action cannot be undone.
    Cancel
  • ashie

    MemberJun 16, 2012

    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
    Are you sure? This action cannot be undone.
    Cancel
  • KACHA PIYUSH

    MemberJun 17, 2012

    // 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);

    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • suraz

    MemberJun 19, 2012

    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
    Are you sure? This action cannot be undone.
    Cancel
  • Sahithi Pallavi

    MemberJun 19, 2012

    yeah, so is there no chance to run a program in jdk1.7 without main()?
    Are you sure? This action cannot be undone.
    Cancel
  • Ankit Niranjan

    MemberSep 30, 2012

    Sada
    guys..! My friend told me that we can write and run a java program without using the main method.
    i.e By using static

    she gave this example program...!

    public void static( )
    public class
    {
    public void static( )
    {
    System.out.println( "Without main method" );
    System.exit(0);
    }
    }
    but hw we can run this program??????
    Are you sure? This action cannot be undone.
    Cancel
  • Sahithi Pallavi

    MemberSep 30, 2012

    Ankit Niranjan
    but hw we can run this program??????
    please go through the above posts. You will find the explanation.
    Are you sure? This action cannot be undone.
    Cancel
  • Extreme Java

    MemberMar 16, 2013

    Yes System.exit() is the right thing to do at the end of static block.
    Are you sure? This action cannot be undone.
    Cancel
  • Rashmi Yadav

    MemberJul 18, 2014

    Sada
    My friend asked me this question.!

    Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?

    Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..

    CAn anyone tell me the reason?
    And correct me If am wrong?
    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberJul 19, 2014

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • Rashmi Yadav

    MemberJul 19, 2014

    Deepika Bansal
    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?
    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.
    Are you sure? This action cannot be undone.
    Cancel
  • madhuri thalluri

    MemberJul 24, 2014

    hello friends..
    I tried the above code in ecllipse..bt its showing error as follows
    so plz hlp me out..
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJul 24, 2014

    madhuri thalluri
    hello friends..
    I tried the above code in ecllipse..bt its showing error as follows
    so plz hlp me out..
    Check the following reply in this thread..
    Anoop Kumar
    Static block trick, no more applicable for JDK 1.7+😏
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register