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:
- find class running
public static void main(string [] args)
method - get
simplename
- 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 correctstacktraceelement
; - 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
Post a Comment