Could you run the java program without main method?

//I googling here i found,some guys executed the program without having main method ..they used static class as an object with System.exit(0)..program will compile but cannot run, program throws an exception..main not found ..share ur ideas if u compile and run both without having main method.

  • Manish Verma
  • Manish Verma
  • |
  • 20 Jun
  • |
  • 20603 Views
  • |
  • 67 Answers

text limit upto 4000

  • 6

    A program can be compiled and executed without main method if we use static initalizer and having System.exit(0); statement at the end lets the program to exit before JVM is looking for main method. However, this can be done only upto JAVA-6 version 
    From Java-7 version the program gets compiled but will not get executed and will show an error message as 
    The program compiled successfully, but main class was not found. Main class should contain method: public static void main (String[] args).

    Servlets and applets do not require main method for their execution.

    • Voted By:
    • Radharaman ,
    • Priyal ,
    • Rajat ,
    • Mohan ,
    • Manish ,
    • Swathi

  • 4

    Hello Manish

    Yes their is a way to run a JAVA program without the main method.
    Code for That
    public class Manish{
    static{
    System.out.println("Hello Youth4Work");
    System.exit(0);
    }
    Now you can compile the program using javac Manish.java
    and after that run it using java Manish.
    It will print the message as expected. But it will show a message 
    "Exception in thread "main" java.lang.NoSuchMethodError: main"
    To avoid this message I have added System.exit(0); after print statement The System.exit(0) lets the program exit before the JVM is looking for the main method.
    One more thing this Up to and including Java 6 it was possible to do this using the Static Initialization block but In Java 7, however, this does not work anymore, even though it compiles, the following error will appear when you try to execute it:
    "The program compiled successfully, but main class was not found. Main class should contain method: public static void main (String[] args)."

    I hope it makes the thing clear.:)

    • Voted By:
    • Shivansh ,
    • Anoop ,
    • Rakshita ,
    • Manish

  • 1

    Yes u can run java without main method , but make sure that your JDK is below 1.7. Above 1.7 its noot supported.

    • Voted By:
    • Manish

  • 0

    Yes we can run the program. For example we have applets which we can execute without main method.


  • 0

    It was possible before jdk 1.7 or before Java 7. Now it'll throw an error as it was a security concern.


  • 0

    No, cannot be done from Java 7.


  • 0

    no


  • 0

    yes you can but make sure that jdk version is < 1.7 because > 1.7 version doesn't supported it.


  • 0

    it was possible before 1.7 version but not now


  • 0

    No , we can't run java program with out main method because interpreter always checks the main method first . Main method indicates where the program should start executing.


  • 0

    yes!! we can compile and execute the java program without main method using "static" block. program is: class Demo{ static{ system.out.println("static block "); system. exit(0); }} Without "system.exit(0);" the program will be compiled but gives runtime error!!


  • 0

    yes we can run java program without main method . if jdk is less than 7th version then with use of static we can run java program .


  • 0

    no


  • 0

    no


  • 0

    Main method is very important for a Java program. But in Java we have applets in which we don't require main method. An applet have a applet body where main method is not much required.


  • 0

    yes we can run without main method by using static block


  • 0

    no


  • 0

    Yes, you can but this was possible till previous version of JDK 7. It's not possible in JDK 7 and above. //Previous versions public class A{ //static initializer block static{ System.out.println("Hello"); } } Output: Hello In JDK 7 and above: Output: Error: Main method not found


  • 0

    Yes, we can run the java program without main method by using static blocks.


  • 0

    In C or c++ u never executing your program without using main function.


Did Not Get The Answer? 
Contact people of Talent-Core Java directly from here