获取Java中使用的调用名称

时间:2022-12-17 21:25:15

I'm trying to find a generic way to replicate this C functionality:

我正在尝试找到一种复制此C功能的通用方法:

int main(int argc, char** argv){
    fprintf(2,"%s: error you did something wrong.\n", argv[0]);
    return 1;
}

in java. So far the only way was to hardcode this into the app, which is ugly. I'd like to get something similar to:

在java中。到目前为止,唯一的方法是将其硬编码到应用程序中,这很难看。我想得到类似的东西:

someObj.getClass().getSimpleName();

inside my static main, without referring back to my own class.

在我的静态主要内部,没有回到我自己的类。

Is this even possible?

这有可能吗?

Edit

My searching for a good hour didn't turn this one up.

我寻找一个好时光并没有把这个变成现实。

Duplicated question, see $0 (Program Name) in Java? Discover main class? for answer

重复的问题,请参阅Java中的$ 0(程序名)?发现主要课程?回答

4 个解决方案

#1


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

用Java看$ 0(程序名)?发现主要课程?

#2


The way java finds the class in which to run static void main() is by the name passed to the java executable (and the classpath). So it's not possible to have main run unless the right name was passed.

java找到运行static void main()的类的方法是传递给java可执行文件(和类路径)的名称。因此,除非通过正确的名称,否则不可能进行主运行。

*Well, unless the main that is found calls something like "FooBar.main()" or uses the class loader to find it.

*好吧,除非找到的main调用类似“FooBar.main()”或使用类加载器来查找它。

Even then, as main is static and static functions aren't virtual, the whatever main is called is called because main was called on that class. So in your code, the name (which inn't passed in the args to main) in just the name of the class that main function is in.

即便如此,由于main是静态函数并且静态函数不是虚函数,因此调用main函数是因为main调用了该类。所以在你的代码中,名称(在args中没有传递给main)只是在main函数所在的类的名称中。

So there's never an error, and so never need to find the name.

所以从来没有错误,因此永远不需要找到名称。

#3


The only thing I can think of is that you determine which Thread is the main thread, get the stack trace of that thread and then walk back to the top of the stack and interrogate the class name and method name of the element to see if it's what you want.

我唯一能想到的是你确定哪个Thread是主线程,获取该线程的堆栈跟踪然后走回堆栈顶部并询问元素的类名和方法名,看它是否是你想要什么。

That's pretty icky though. I'd reconsider the original requirement and see if there's not a more straight-forward means of achieving what you want.

虽然那很蠢。我会重新考虑原始要求,看看是否没有更直接的方法来实现你想要的东西。

#4


I don't think this is possible. The easiest way to print this is using MyClass.class.getSimpleName(), or you could create (not throw) a Throwable, print the stack trace, and go from there.

我不认为这是可能的。打印它的最简单方法是使用MyClass.class.getSimpleName(),或者你可以创建(不抛出)Throwable,打印堆栈跟踪,然后从那里开始。

#1


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

用Java看$ 0(程序名)?发现主要课程?

#2


The way java finds the class in which to run static void main() is by the name passed to the java executable (and the classpath). So it's not possible to have main run unless the right name was passed.

java找到运行static void main()的类的方法是传递给java可执行文件(和类路径)的名称。因此,除非通过正确的名称,否则不可能进行主运行。

*Well, unless the main that is found calls something like "FooBar.main()" or uses the class loader to find it.

*好吧,除非找到的main调用类似“FooBar.main()”或使用类加载器来查找它。

Even then, as main is static and static functions aren't virtual, the whatever main is called is called because main was called on that class. So in your code, the name (which inn't passed in the args to main) in just the name of the class that main function is in.

即便如此,由于main是静态函数并且静态函数不是虚函数,因此调用main函数是因为main调用了该类。所以在你的代码中,名称(在args中没有传递给main)只是在main函数所在的类的名称中。

So there's never an error, and so never need to find the name.

所以从来没有错误,因此永远不需要找到名称。

#3


The only thing I can think of is that you determine which Thread is the main thread, get the stack trace of that thread and then walk back to the top of the stack and interrogate the class name and method name of the element to see if it's what you want.

我唯一能想到的是你确定哪个Thread是主线程,获取该线程的堆栈跟踪然后走回堆栈顶部并询问元素的类名和方法名,看它是否是你想要什么。

That's pretty icky though. I'd reconsider the original requirement and see if there's not a more straight-forward means of achieving what you want.

虽然那很蠢。我会重新考虑原始要求,看看是否没有更直接的方法来实现你想要的东西。

#4


I don't think this is possible. The easiest way to print this is using MyClass.class.getSimpleName(), or you could create (not throw) a Throwable, print the stack trace, and go from there.

我不认为这是可能的。打印它的最简单方法是使用MyClass.class.getSimpleName(),或者你可以创建(不抛出)Throwable,打印堆栈跟踪,然后从那里开始。