Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

With out Main()

 
Srinivas Kongara
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How to execute a java program without Main()?

Please give example code.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15274
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would argue that that isn't a program.
 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ThankYou
 
Srinivas Kongara
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


ThankYou
 
irrfan jamadar
Greenhorn
Posts: 10
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall move this discussion to where it belong
 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic