• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to know which was the starting main-class of the application  RSS feed

 
Puneet Nayyar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've a peculiar situation here. There are two ways in which I can start my application. Either I can directly call main class M1 or I can do some pre-checks in main-class M2 first and and then call M1.main() from M2. Now in some another third class I need to check which class was the entry point for the application i.e. i want to know whether my application came up through class M1 or class M2. Is there any way I can do this?
Any help would be highly appreciated.

Thanks & Regards,
Puneet
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't sound like standard object-oriented architecture; a class or object ought not to "know" where it was invoked from.
You could try writing a text file with the name of the class starting off, and reading it later. There are bound to be lots of other ways to do it.
 
Puneet Nayyar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion Ritchie.

Sorry I didn't mention this earlier but the thing is, both the main classes are not in scope of my module (class 3) so I cannot add any logic in them to check the way you have suggested. So I just wanted to learn if Java provides any functionality through meta classes etc. where we can see the invoking point of the application.

I hope I am able to express the problem clearly.

Thanks again!!
Puneet
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your code is not executed from another thread, you can investigate the stack trace:
Of course this does not work this way if the code is executed in a different thread. Thread.getAllStackTraces() could perhaps help you then, but you will have to identify the main thread yourself.
 
Puneet Nayyar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys!!

Best Regards,
Puneet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!