How to determine main class at runtime in threaded java application?

后端 未结 10 982
野的像风
野的像风 2021-01-01 13:37

I want to determine the class name where my application started, the one with the main() method, at runtime, but I\'m in another thread and my stacktrace doesn\'t go all the

10条回答
  •  悲&欢浪女
    2021-01-01 14:29

    I suggest to put this information into a system property. This is usually simple to do when you start your application from a script.

    If you can't do that, I suggest to set the property in the main() method of every application. The most simple way here would be to have every app derive it's "main class" from a common base class and run an init step in there. I often do this for command line processing:

    public class Demo extends Main {
        main(String[] args) {
            Main._main(new Demo (), args);
        }
    
        // This gets called by Main._main()
        public void run (String[] args) {
        }
    }
    

提交回复
热议问题