Trick : Java program with out main

Written By | 4 October 2009| 5 Comments

Writing a java program with out main thread.

Tricky way to compile a java program with out any main

class sample
{
static
{
System.out.println("Hello Word !!");
}
}

Share With Others

Similar Articles

    None Found

5 Comments »

  • Charlie (Colorado) said:

    Well, okay, but it terminates with an exception.

    69 $ cat > sample.java
    class sample
    {
    static
    {
    System.out.println(“Hello Word !!”);
    }
    }
    70 $ javac sample.java
    71 $ java sample
    Hello Word !!
    Exception in thread “main” java.lang.NoSuchMethodError: main
    72 $

  • Chris said:

    So you get your output, and then it crashes… good one

    Hello Word !!
    java.lang.NoSuchMethodError: main
    Exception in thread “main”

    This is not a trick, it’s just bad programming, that any IDE would pick up automatically by the way.

  • Craig Baker said:

    This makes no sense. You don’t need a main method to compile a java class.

  • Nyarlathotep said:

    “Writing a java program with out main thread.”
    If there wouldn’t be a thread, there wouldn’t be *any* execution of code

    “Tricky way to compile a java program with out any main ”
    All my classes compile without a main, but they won’t run without it

    Btw: I don’t see any sense here… and if try to run a mulithreaded Server like this and you will fail.

  • Jens said:

    The naming for the class is wrong. It should be Sample (upper case S).

Leave your response!