aspose file tools*
The moose likes Java in General and the fly likes Running a Java programme without main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running a Java programme without main " Watch "Running a Java programme without main " New topic
Author

Running a Java programme without main

kulbhushan patil
Greenhorn

Joined: Aug 10, 2010
Posts: 6

Hi.... can anyone tell me how to run java programme without main method in it ?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

No, you can't do that. If you want to print message then you have to use static initialization block but after execution of block exception will be throw as NoSuchMethodError: main,

And welcome to Javaranch


Life is easy because we write the source code.....
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I think you can do the following, but I wouldn't be complimentary of anyone that tried it...
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Pramod P Deore wrote:No, you can't do that. If you want to print message then you have to use static initialization block but after execution of block exception will be throw as NoSuchMethodError: main,

Not if you exit before that ;)kulbhushan, why do you think you need to do this? (Hint: you don't)

edit I like David's sysout better!


luck, db
There are no new questions, but there may be new answers.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I think I hated this idea so much that I blogged about it once, but I'll save the rant for therapy ;)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Any good manager will fire you on the spot if you ever use that trick in anything other than example code. After all, if the class is loaded it will execute the code in the static block and then terminate the JVM. This will also happen if I ever try to instantiate NoMain (new NoMain()) or call a static method on it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
As stated before you need a main method if running a "standard" standalone Java program.

But quite often Java application run inside a container that defines a lifecycle by calling special methods. E.g. if you run an applet the methods init, start, stop and destroy are called at specific times. (If you add a main method you could run an applet both within an applet container and standalone.) Another example for an application within a container and with a lifecycle would be a web application that runs within a server, e.g. Tomcat.

If the container is written in Java, though, there still will be a main method ;-).
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
well, the weirdest code that anyone would write (of, I just did)


Well, this is just for fun and has nothing to do with developing complex app using this sample.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Running a Java programme without main