This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Can we write a java program without main method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can we write a java program without main method?" Watch "Can we write a java program without main method?" New topic
Author

Can we write a java program without main method?

Shrikanth Nekkanti
Ranch Hand

Joined: Jul 12, 2007
Posts: 48
can we write a java program without using main() method.
If yes can you explain how is it pdssible.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

applets do not have a main() method.

There is also a hack where you use an initializer block (i think) and a System.exit() call, but i wouldn't really call it a 'program'.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Les Hayden
Greenhorn

Joined: Jun 19, 2002
Posts: 28
I suppose to answer your question you really have to define what you mean by a
program.
If you mean a stand-alone Java application, then no. The JVM looks for a method public static void main(String[]) as an entry to the application. Failing to find that, it will throw an exception and terminate.

However, there are several other types of applications that do not use the main() method. Web-applications and applets are a couple that come to mind. However, they have special JVMs that allow them to run. In the first case, web applications run in a Java container we commonly call a server. In the second case the application runs in a browser plug-in.


Les Hayden
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

There's an old Java parlor trick that's done using mirrors... Uh, I mean static blocks.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 63

this above code is not executing in java 7 why?

with regards,
Shivdhway Pandey
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Because there was a change in Java 7 so that it checks if there's a main() method before it actually runs the program.

Discussions about how to write a Java program without a main() method happen regularly here - if you search the forums, you'll find lots of discussions about this topic.

Knowing how to write a program without a main() method is not really of any practical value - you'd never use clever tricks like these for any serious piece of software.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Shrikanth - just out of interest, why did you want to know this ? This question gets asked a lot, but it's always seemed to me to be like going into a car showroom and asking the salesman - if I remove all the wheels from the car will I still be able to drive it around ? Cars are designed to need wheels, Java programs are designed to need a main method - why worry about whether they will work without them ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7718
    
  20

Stuart A. Burkett wrote:Shrikanth - just out of interest, why did you want to know this ?

Nice reply, but sadly I suspect Shrikanth left the room a while ago (check the date ).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Winston Gutkowski wrote:
Stuart A. Burkett wrote:Shrikanth - just out of interest, why did you want to know this ?

Nice reply, but sadly I suspect Shrikanth left the room a while ago (check the date ).

Winston

Whoops. Guess I'll just have to wait for someone else to ask this question and then ask them. Probably shouldn't be too long
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Stuart A. Burkett wrote:
Winston Gutkowski wrote:
Stuart A. Burkett wrote:Shrikanth - just out of interest, why did you want to know this ?

Nice reply, but sadly I suspect Shrikanth left the room a while ago (check the date ).

Winston

Whoops. Guess I'll just have to wait for someone else to ask this question and then ask them. Probably shouldn't be too long

http://www.coderanch.com/t/589431/Java/java/program-without-main
Your wish is granted. Long live Jambi.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
I saw that but thought it was more about why you can do it in Java 6 and not in Java 7 rather than whether it was actually possible. In fact it was started by the same person who reawakened this zombie.
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 63

if static block is removed in java7 then whats in place of it or new in place of it?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

shivdhwaj pandey wrote:if static block is removed in java7 then whats in place of it or new in place of it?


But that question assumes that "static block is removed in java 7". Why did you assume that?
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 63

Paul Clapham wrote:

But that question assumes that "static block is removed in java 7". Why did you assume that?

Because when i try to run program without main () it shows error, but concept says it has to run
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41629
    
  55
Not sure what you mean by "concept", but as Paul implies, static blocks continue to be available. And Jesper already mentioned why this strange idiom no longer works in Java 7. (Arguably it shouldn't ever have worked, but that's a different subject.)


Ping & DNS - my free Android networking tools app
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 63

shivdhwaj pandey wrote:whats new in place of it?

how we can do so in java7
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41629
    
  55
You can't. That you could before was arguably a bug, and it has now been fixed. End of story.
shivdhwaj pandey
Ranch Hand

Joined: Nov 12, 2011
Posts: 63

Ulf Dittmer wrote:You can't. That you could before was arguably a bug, and it has now been fixed. End of story.

OOOOOOOOOOOkkkkkkkkkkkkkkkkkkkkkkk
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we write a java program without main method?