如何在线程java应用程序中确定运行时的主类? [英] How to determine main class at runtime in threaded java application?

查看:132
本文介绍了如何在线程java应用程序中确定运行时的主类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时确定我的应用程序启动的类名,带有main()方法的类名,但我在另一个线程中,我的堆栈跟踪不会一直回到原始类。

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 way back to the original class.

我搜索了系统属性以及ClassLoader必须提供的所有内容,并且没有提供任何内容。这些信息是不可用的吗?

I've searched System properties and everything that ClassLoader has to offer and come up with nothing. Is this information just not available?

谢谢。

推荐答案

我弄清楚了。任何人都可以告诉我这个环境变量是否总是在操作系统的其他Java实现中出现?这在Oracle JVM上产生类似org.xyClassName的字符串

I figured it out. Can anyone tell me if this environment variable will always be around in other java implementations across operating systems? This on Oracle JVM yields a String like "org.x.y.ClassName"

public static String getMainClassName() {
  for (final Map.Entry<String, String> entry : System.getenv().entrySet())
    if (entry.getKey().startsWith("JAVA_MAIN_CLASS")) // like JAVA_MAIN_CLASS_13328
      return entry.getValue();
  throw new IllegalStateException("Cannot determine main class.");
}

这篇关于如何在线程java应用程序中确定运行时的主类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆