• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we write a java program without main method?

 
Shrikanth Nekkanti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we write a java program without using main() method.
If yes can you explain how is it pdssible.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
Les Hayden
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's an old Java parlor trick that's done using mirrors... Uh, I mean static blocks.
 
shivdhwaj pandey
Ranch Hand
Posts: 67
Java PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this above code is not executing in java 7 why?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10430
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Java PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if static block is removed in java7 then whats in place of it or new in place of it?
 
Paul Clapham
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Java PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
shivdhwaj pandey
Ranch Hand
Posts: 67
Java PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivdhwaj pandey wrote:whats new in place of it?

how we can do so in java7
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. That you could before was arguably a bug, and it has now been fixed. End of story.
 
shivdhwaj pandey
Ranch Hand
Posts: 67
Java PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:You can't. That you could before was arguably a bug, and it has now been fixed. End of story.

OOOOOOOOOOOkkkkkkkkkkkkkkkkkkkkkkk
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic