简体   繁体   English

我如何确定哪个类被执行为主类?

[英]How do I figure out which class was executed as the main class?

Ultimately what I want is something similar to the answer from this question: 最终,我想要的是类似于该问题的答案:

Find where java class is loaded from 查找从何处加载Java类

There's one twist. 有一个转折。 Unlike the examples, I don't know what class will contain the main method at compile time. 与示例不同,我不知道在编译时哪个类将包含main方法。 But if I knew which class was called first, I could then locate where my application was executed from. 但是,如果我知道首先调用哪个类,则可以找到执行我的应用程序的位置。

So my question is this: How do I discover what class contains the main method that was called to execute my java application? 所以我的问题是:如何发现哪个类包含执行Java应用程序所调用的主要方法?

EDIT (Clarification): 编辑(澄清):

I want to have a library to know the "name" of the application I'm running. 我想让一个图书馆知道我正在运行的应用程序的“名称”。 So if I type in java -cp ... MyMainClass and my main class happened to live in the folder /usr/something/myApp/lib/MyMainClass.jar then I could report back "myApp". 因此,如果我输入java -cp ... MyMainClass,而我的主类恰好位于文件夹/usr/something/myApp/lib/MyMainClass.jar中,那么我可以报告“ myApp”。 This would be a fallback default option to infer my program's name. 这是推断我程序名称的后备默认选项。

If you're in the main thread of the program, you can do this: 如果您在程序的主线程中,则可以执行以下操作:

StackTraceElement[] stackTrace = new Exception().getStackTrace();
String className = stackTrace[stackTrace.length - 1].getClassName();

As @Makoto says, it might be useful with an explanation of why you need to do this; 正如@Makoto所说,它可能对解释为什么需要执行此操作很有用; there are probably better ways, depending on how you're starting the application. 可能有更好的方法,具体取决于您启动应用程序的方式。

To get the entry point, not depending on the thread, you can retrieve all stacktraces and choose the top point in the one with id == 1 , for example (Java 8): 要获取入口点,而不依赖于线程,可以检索所有堆栈跟踪,并在id == 1那一个中选择最高点,例如(Java 8):

StackTraceElement[] stackTrace = Thread.getAllStackTraces()
        .entrySet()
        .stream()
        .filter(entry -> entry.getKey().getId() == 1)
        .findFirst().get()
        .getValue();
System.out.println(stackTrace[stackTrace.length - 1]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据教授需要弄清楚如何操纵变异子以找出一个类,我对此感到困惑 - need to figure out how to manipulate the mutator to figure out a class according to professor, I am confusd on how to do that 如何确定一个类名所指示的内部匿名类? - how to figure out which inner anonymous class indicated by a class name? 在 JDeveloper 中,如何指定要使用哪个类的“main”? - In JDeveloper, how do I specify which class's 'main' to use? 如何找出哪个Java实例已加载类 - How to figure out which instance of java has a class loaded 如何将菜单栏类添加到主类中的JFrame中 - How do I add my menubar class to my JFrame which is in my Main Class 您如何确定 CLASS 是否是 spring 代理? - How do you figure out whether a CLASS is a spring proxy? 我不断收到错误消息:无法找到或加载主类,无法找出原因? - I keep getting Error: Could not find or load main class and cant figure out why? 无法弄清楚如何运行我的jar文件“找不到或加载主类” - Can't figure out how to run my jar file “could not find or load main class” 我如何告诉 Spring 引导哪个主要 class 用于可执行文件 jar? - How do I tell Spring Boot which main class to use for the executable jar? 如何使我的JFrame类成为主类? - How do I make my JFrame class my main class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM