如何知道调用的java程序的父类?
我正在开发一个分布式应用程序,并在类的主要方法中遇到异常。我如何知道哪个java程序调用了它?我尝试调试分布式应用程序,但无法弄清楚。
I am working on a distributed application and getting an exception in the main method of a class. How do I know which java program has invoked it ? I tried debugging the distributed application, but could not figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法知道是什么从 ClassB 调用了 java 进程。异常只会深入到它自己的进程的调用堆栈。如果有其他东西启动了该进程,即使是 java 本身,也无法从 ClassB 知道这一点。
您最好使用 ClassA 的有用日志记录(调试/信息消息和异常堆栈跟踪)。您必须确保 ClassB 在失败时正确退出(以 0 以外的代码退出),然后 ClassA 可以在它生成的进程中看到此失败。
You cannot know what invoked the java process from ClassB. The Exception will only go as deep as it's own call stack from it's process. If something else started the process, even if it was java itself, there is no way of know this from ClassB.
You are better off using helpful logging (of both debug/info messages and exception stacktraces) from ClassA. You will have to make sure that ClassB exits appropriately when it fails (exit with a code other than 0) and then ClassA can see this failure in the process it spawned.
尝试获取当前的堆栈跟踪并查看谁调用了您的 main 方法。
Try getting the current stacktrace and see who calls your main method.
以下是“Exception”的 Javadoc:
http://download .oracle.com/javase/6/docs/api/java/lang/Exception.html
除了调用“getMessage()”之外,您还可以对异常对象执行多种操作。例如,您可以“printStackTrace()”(显示它是如何被调用的,以及它被抛出的位置)。
Here's the Javadoc for "Exception":
http://download.oracle.com/javase/6/docs/api/java/lang/Exception.html
There are several things you can do with the exception object besides call "getMessage()". For example, you can "printStackTrace()" (showing you how it was called, and where it was thrown).