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 !!");
}
}
Similar Articles
- None Found









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 $
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.
This makes no sense. You don’t need a main method to compile a java class.
“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.
The naming for the class is wrong. It should be Sample (upper case S).
Leave your response!
Subscribe to BrownTips
Subscribe to RSS Feeds
Translator
Ads
Recent Posts
Most Commented
Most Viewed