Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Can we write a java program without main method?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can we write a java program without using main() method.
If yes can you explain how is it pdssible.
 
lowercase baba
Posts: 13001
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's an old Java parlor trick that's done using mirrors... Uh, I mean static blocks.
 
Ranch Hand
Posts: 67
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this above code is not executing in java 7 why?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if static block is removed in java7 then whats in place of it or new in place of it?
 
Marshal
Posts: 26749
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic