$0 (Program Name) in Java? Discover main class?

前端 未结 8 973
死守一世寂寞
死守一世寂寞 2020-12-03 00:23

Is there a way to find the name of the program that is running in Java? The class of the main method would be good enough.

8条回答
  •  猫巷女王i
    2020-12-03 00:57

    Or you could just use getClass(). You can do something like:

    public class Foo
    {
        public static final String PROGNAME = new Foo().getClass().getName();
    }
    

    And then PROGNAME will be available anywhere inside Foo. If you're not in a static context, it gets easier as you could use this:

    String myProgramName = this.getClass().getName();
    

提交回复
热议问题