Trick : Java program with out main

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 !!");
}
}
Posted By

6 Responses to “Trick : Java program with out main”

  1. Charlie (Colorado) says:

    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 $

  2. Chris says:

    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.

  3. Craig Baker says:

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

  4. Nyarlathotep says:

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

  5. Jens says:

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

  6. mikeadmin says:

    Hi,
    if you dont want to exit with an exception then write the program in this way……
    class sample
    {
    static
    {
    System.out.println(“Hello Word !!”);
    System.exit(0);
    }
    }

    if you use System.exit(0); then it will not go into the main method and there cant be any exception.

Leave a Reply




*