如何调试位于堆上的对象?(eclipse-java)

发布于 2024-12-08 15:27:58 字数 103 浏览 6 评论 0原文

我正在尝试调试位于 jar 上的类,但我无法“看到”显示选项中的对象!

有人告诉我,原因可能是此类中的对象位于堆上而不是堆栈上。

如何在调试模式下访问这些对象?

I'm trying to debug classes that seat on a jar and i cant "see" the objects in display option!

Someone told me that reason can be that objects in this class are located on the heap and not on the stack.

How can I access these objects in debug mode?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

童话里做英雄 2024-12-15 15:27:58

您不调试对象或类。您调试方法。在要调试的对象的方法中放置一个断点,当调用该方法时,您将能够检查该对象的内容。

如果您有一个包含对象 B 集合的对象 A,请在 A 的方法中放置一个断点,然后在调用此方法时您将能够检查其 B 集合。

但是您不能只是从调试开始并查看堆上的所有对象。识别您正在寻找的人可能非常困难。

You don't debug objects or classes. You debug methods. Put a breakpoint inside a method of the object you want to debug, and you'll be able to inspect the contents of this object when this method is called.

If you have an object A containing a collection of objects B, put a breakpoint in a method of A, and you'll be able to inspect its collection of Bs when this method is called.

But you can't just start in debug and see all the objects on the heap. It could be very difficult to identify the one you're looking for.

怀里藏娇 2024-12-15 15:27:58

首先,你混淆了一些概念。对象实例分配在所谓的堆内存(也称为堆)上。方法调用(在这些实例上)被放置在堆栈内存空间中,也称为堆栈上。查阅 Java(和通用 OOP)术语中的堆和栈内存概念,以获得更好的理解。

其次,当您添加断点,在 Eclipse 中调试程序,并且执行命中该断点,并且显示 Eclipse 调试透视图时,您应该注意多个方面:

  • 您可以看到当前实例的全局(类级别)字段及其值(位于视图右上角的“变量”选项卡中。您还可以查看正在调试的方法本地的变量+这些变量的值。

  • 您还可以看到堆栈(在视图的右上角),也就是放置在堆栈空间中的方法链,这些方法在调试器停止的当前方法之前被调用。

  • 当您调试自己的类之一时,您将清楚地看到代码中的哪一点(即视图下部的大窗口) - 这是因为该类的源代码可用于蚀。但是,如果您继续调试,您可能会偶然发现一个方法调用,该方法调用进入您没有源代码的类的实例(例如,如果您的项目使用某些外部 jar,并且您当前的方法调用使用某些方法)该外部 jar 中的类的实例)。此时,Eclipse 无法再在视图的下半部分向您显示“漂亮”的代码,它只是向您显示一些混乱的内容(它是编译后的字节代码)以及一个按钮,可让您附加(如果有)源代码那个外部jar,这样就可以正常显示了。如果您使用“附加源”按钮,您可以浏览并选择 jar 的源。对于大多数(至少是好的)jar 库,您还会获得一个带有源代码的单独 jar,您应该下载该 jar 并将其附加到实际的 jar 中,以便能够对其进行调试。

First off you're jumbling some notions. Objects instances are allocated on what's called Heap memory, aka on the heap. Method calls (on those instances) are placed in the stack memory space, aka on the stack. Look up the notions of heap and stack memory in the Java (and general OOP) jargon to get a better idea.

Second, when you add a break-point, debug your program in Eclipse, and that breakpoint is hit by the execution, and the Eclipse Debug perspective is shown, there's multiple aspects that you should be aware of:

  • you can see the current instance's global (class level) fields and their values (in the upper right corner of your view, in the "variables" tab. You can also see the variables+values of those variables local to the method you are debugging.

  • you can also see the stack (in the upper right corner of the view), aka the chain of methods placed in the stack space that were called prior to the current method where the debugger stopped.

  • when you debug one of your own classes, you'll clearly see at what point in the code you are (that's the big window in the lower part of the view) - that's because the source code of that class is available to Eclipse. However if you continue your debugging, you might stumble upon a method call that goes into the instance of a class who's source code you don't have (like if your project uses some external jar, and your current method call uses some method of some instance of a class in that external jar). At this point Eclipse can no longer show you the "pretty" code in the lower part of the view, it just shows you some jumble (it's the compiled byte code) and also a button that lets you attach (if available) the sources of tha external jar, so that it can be displayed properly. IF you use that "attach sources" button you can browse around and select the sources of your jar. For most (at least the good ones) jar libraries, you also get a separate jar with the sources, which you should download and attach to the actual jar in order to be able to debug it.

美煞众生 2024-12-15 15:27:58

对于没有代码的类,您可以使用
http://sourceforge.net/projects/jadclipse/ 它将反编译类文件并向您显示代码。您可以调试、查看变量值、控制流程,但不能进行任何编辑。

另一种选择是附加源存档,用于您需要调试的 jar。但在这种情况下,您也无法进行编辑。

大多数情况下,您不会在第三方 jar 中进行更改。如果确实有必要,请获取源代码(如果有)。

另一种方法是,一旦类被反编译,将 java 文件中的内容复制到项目中的同一路径下。现在这个类将从你的代码而不是 jar 中选取,你也可以
做出改变

For the classes for which you don't have code , you can use
http://sourceforge.net/projects/jadclipse/ which will decompile the class file and show u the code. You can debug , see variables values , control flow but can't make any edits.

One more option is to attach source archive, for the jar which you need to debug. But in this case too u can't make edits.

Mostly you will not be making changes in third party jars. In case its really necessary get the source code if its available.

One more way is once class is decompiled, copy the content in a java file under same path in your project. Now this class will be picked from u r code instead of jar and you can also
make changes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文