GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes All Java projects must use main method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "All Java projects must use main method?" Watch "All Java projects must use main method?" New topic
Author

All Java projects must use main method?

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
All Java projects must use main method?

André AS
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Are all Java projects stand-alone applications?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bob Livingston
Greenhorn

Joined: Apr 06, 2010
Posts: 4
Yes, all java programs must have a main method. The static main is the first method called in any java program. It does not matter where it occurs it just must occur.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

Bob,

the original post asked about all java PROJECTS, not programs. I have worked on many PROJECTS that didn't have a main.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Bob Livingston wrote:Yes, all java programs must have a main method.

Define "Java program". Is a web application also a Java program? Or an applet?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66



[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Shine Tom
Greenhorn

Joined: Sep 17, 2012
Posts: 19
fred rosenberger wrote:Bob,

the original post asked about all java PROJECTS, not programs. I have worked on many PROJECTS that didn't have a main.


Hello Fred, Can you tell me why many java PROJECTS doesnt contain a main function. Please also tell me when there is no main method, how is the application executed.
Many Thanks,
Shine
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

Applets don't use a main method to initialize, they provide an init() method.
Likewise, Servlets don't require a main method - they too provide life-cycle methods that are invoked by a servlet container / application server.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Shine Tom
Greenhorn

Joined: Sep 17, 2012
Posts: 19
Thanks for the update. Alos one more doubt, what about in the case of eclipse plugins ? do they need a main method??
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Shine Tom wrote:Thanks for the update. Alos one more doubt, what about in the case of eclipse plugins ? do they need a main method??


No. Which a quick Google search would have told you.

Also, libraries don't need a main().

Also, any of these projects that don't need a main() could still have one, or several, for testing purposes, or if they're capable of being run in more than one context. This is somewhat unusual, but not unheard of.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

No, much like a Servlet an Eclipse plug-in provides life-cycle methods, wether its target runtime environment is Eclipse itself or an OSGi framework (Equinox). This is typically how plugins work. There's actually a pretty neat JavaRanch journal entry on adding plugins to a Java application, written by Ulf Dittmer.

Edit: Dude seriously? Nice timing
Shine Tom
Greenhorn

Joined: Sep 17, 2012
Posts: 19
Thanks Jeff & Jelle..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: All Java projects must use main method?