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

后端 未结 10 991
野的像风
野的像风 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:18

    The JAVA_MAIN_CLASS environment value isn't always present depending on the platform. If you just want to get a name of the "main" class that started your Java process, you can do this:

      public static String getMainClassName()
      {
        StackTraceElement trace[] = Thread.currentThread().getStackTrace();
        if (trace.length > 0) {
          return trace[trace.length - 1].getClassName();
        }
        return "Unknown";
      } 
    

提交回复
热议问题