File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running a Java programme without main

 
kulbhushan patil
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi.... can anyone tell me how to run java programme without main method in it ?
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can do the following, but I wouldn't be complimentary of anyone that tried it...
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic