• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Running a Java programme without main

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi.... can anyone tell me how to run java programme without main method in it ?
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can do the following, but I wouldn't be complimentary of anyone that tried it...
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ;)
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ;-).
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic