Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

With out Main()

 
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.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • 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.
 
lowercase baba
Posts: 12870
62
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: 343
Mac OS X Spring Java
  • 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!! ;)
 
Marshal
Posts: 69752
277
  • 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
 
Greenhorn
Posts: 10
Tomcat Server Java
  • 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
 
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
Marshal
Posts: 69752
277
  • 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: 343
Mac OS X Spring Java
  • 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
Marshal
Posts: 69752
277
  • 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: 343
Mac OS X Spring Java
  • 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!!
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic