java - How to get default application name in cli? -


i want create framework shows application name on statup. targeting command line interface applications.

question: how can such application name in generic way?

eg spring offers property, not set default:

@value("${spring.application.name}") private string appname; 

and don't want set property explicit. looking kind of "default application name".

in java ee container there following option:

string myapplicationname = (string) initialcontext.lookup("java:app/appname");   

but how cli apps? how can kind of generic application name?

the closest can get, if interpreted correctly question, to:

  1. find class running public static void main(string [] args) method
  2. get simplename
  3. store aforementioned name system property

and in order so, have 2 options:

  • call thread.currentthread().getstacktrace(), , inspect tail element. has executed in main thread well, otherwise wont retrieve correct stacktraceelement;
  • call thread:getallstacktraces(), , parse entire map identify main thread, corresponding value, , pick last stacktraceelement

once have stacktraceelement, can call stacktraceelement:getclassname() return like

scala.tools.nsc.maingenericrunner

split string, save system property, , you're go.

hope you.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -