| Author |
With out Main()
|
Srinivas Kongara
Greenhorn
Joined: May 07, 2012
Posts: 7
|
|
How to execute a java program without Main()?
Please give example code.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
This question has been asked before; if you do a search, you'll find previous discussions about this topic.
The standard trick doesn't work anymore on Java 7.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Palak Mathur
Ranch Hand
Joined: Jan 29, 2007
Posts: 303
|
|
kongara srinu wrote:
How to execute a java program without Main()?
Please give example code.
Just write a static block. Something like this
Some people say it works till Java 6, but for me it doesn't works even for 6. I have tried this code till Java 5.
|
Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9946
|
|
|
I would argue that that isn't a program.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Palak Mathur
Ranch Hand
Joined: Jan 29, 2007
Posts: 303
|
|
fred rosenberger wrote:I would argue that that isn't a program.
If you will say so, then I will agree with you!! ;)
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
The nearest you get to that being a program is magic, which is false ⟹ skip. That is classified as a program in Generalised Substitution Language, but it is called an “extreme substitution” because it is never feasible.
You can of course put other things in the static block before the exit call.
Of course, as we have already been told, the newer versions of the JVM appear to seek the main method before executing that static initialiser, so they throw the Exception first and fail to execute even the initialiser.
|
 |
Srinivas Kongara
Greenhorn
Joined: May 07, 2012
Posts: 7
|
|
|
ThankYou
|
 |
Srinivas Kongara
Greenhorn
Joined: May 07, 2012
Posts: 7
|
|
Palak Mathur wrote:
kongara srinu wrote:
How to execute a java program without Main()?
Please give example code.
Just write a static block. Something like this
Some people say it works till Java 6, but for me it doesn't works even for 6. I have tried this code till Java 5.
ThankYou
|
 |
Srinivas Kongara
Greenhorn
Joined: May 07, 2012
Posts: 7
|
|
ThankYou
|
 |
irrfan jamadar
Greenhorn
Joined: May 09, 2012
Posts: 9
|
|
Static block runs once and it is before program enter into main().
This block may be used to check the requisite h/w and s/w requirement for successful execution of main program. Or it may be used to give to load requisite4 driver for main program
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5852
|
|
irrfan jamadar wrote:Static block runs once and it is before program enter into main().
Except that this is apparently no longer the case in Java 7.
This block may be used to check the requisite h/w and s/w requirement for successful execution of main program.
No, that would not be a good idea. If you need to do that, just do it from main().
Generally the only job of your main class should be to prepare the app for execution and then start that execution. Anything you need to do in that class should be done in the main() method or in methods called by main(). There's probably never any reason to use a static initializer block in that class.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
I shall move this discussion to where it belong
|
 |
Palak Mathur
Ranch Hand
Joined: Jan 29, 2007
Posts: 303
|
|
Campbell Ritchie wrote:I shall move this discussion to where it belong 
You mean to the correct thread or the right direction?
P.S.:- This is my 50th post!!
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
Look at the top of the page, and you will see where I think the right direction is
And congratulations on the 50. I must be a lot older than you.
|
 |
Palak Mathur
Ranch Hand
Joined: Jan 29, 2007
Posts: 303
|
|
Campbell Ritchie wrote:Look at the top of the page, and you will see where I think the right direction is
And congratulations on the 50. I must be a lot older than you.
Thank you!!
Ok!! You mean Programming Diversion!!
Older in terms of post - yes!!
|
 |
 |
|
|
subject: With out Main()
|
|
|