如何在第三方库中设置Eclipse的断点?

时间:2023-02-05 11:31:54

I'm getting a NullPointerException in a Class from a 3rd party library. Now I'd like to debug the whole thing and I would need to know from which object the class is held. But it seems to me that I cannot set a breakpoint in a Class from a 3rd party.

我正在从第三方库中获得一个NullPointerException。现在我想要调试整个过程,我需要知道这个类的对象是什么。但在我看来,我不能在一个第三方的课堂上设置一个断点。

Does anyone know a way out of my trouble? Of course I'm using Eclipse as my IDE.

有人知道我的麻烦吗?当然,我使用Eclipse作为IDE。

Update: the library is open-source.

更新:图书馆是开源的。

6 个解决方案

#1


32  

The most sure-fire way to do this (and end up with something that's actually useful) is to download the source (you say that it is open-source), and set up another "Java Project" pointing at that source.

最可靠的方法是下载源代码(您说它是开源的),并设置另一个指向该源的“Java项目”。

To do that, get the source downloaded and unzipped somewhere on your system. Click "File"->"New"->"Java Project". In the next dialog, give it a project name and select "Create Project from Existing Source". Browse to the root location of the open source library.

要做到这一点,请将源代码下载并解压到系统的某个位置。点击“文件”- >“新”- >“Java项目”。在下一个对话框中,给它一个项目名称并选择“从现有的源创建项目”。浏览到开源库的根位置。

Supposing that all the additional libraries that are required by the project and such are included in the project you downloaded, Eclipse will figure everything out and set the build path up for you.

假设项目所需的所有附加库都包含在您下载的项目中,Eclipse将为您确定一切并设置构建路径。

You'll need to remove the open source jar from your project's build path, and add this new project to the build path of your project.

您将需要从项目的构建路径中删除开放源码jar,并将这个新项目添加到项目的构建路径中。

Now, you can just treat this as your code, and debug at will.

现在,您可以将此作为您的代码,并随意调试。

This gets around at least a couple of problems with other approaches:

这至少解决了其他方法的几个问题:

  1. You could "attach source" to the jar file, but if the jar file was compiled without debug information, this still won't work. If the jar file was compiled with debug information (lines,source,vars...see http://java.sun.com/j2se/1.3/docs/tooldocs/win32/javac.html, and the -g option).

    您可以“附加源代码”到jar文件,但是如果没有调试信息编译jar文件,这仍然不能工作。如果用调试信息编译jar文件(行、源、vars…)请参阅http://java.sun.com/j2se/1.3/docs/tooldocs/win32/javac.html,以及-g选项)。

  2. You could add an "exception breakpoint" to see when the NullPointerException is raised, but that's a common exception, and may well get raised and dealt with many (hundreds of?) times prior to the one you're looking for. Plus, without the original source, you won't be able to really see much of anything about the code that is throwing the NullPointerException - the likelihood you'll be able to figure out what's wrong is pretty low.

    您可以添加一个“异常断点”,以查看何时引发NullPointerException,但这是一个常见的异常,并且很可能在您正在查找的异常之前被多次(数百次?)提出和处理。另外,如果没有原始的源代码,您将无法真正看到抛出NullPointerException的代码的任何内容—您能够找出错误的可能性非常低。

#2


59  

You can easily set method breakpoints in 3rd party libraries without having the source. Just open the class (you'll get the "i-have-no-source" view). Open the outline, right-click on the method you want and click on Toggle Method Breakpoint to create the method breakpoint.

您可以轻松地在第三方库中设置方法断点,而不需要源代码。打开这个类(你会看到“i-have-no-source”视图)。打开outline,右键单击您想要的方法,然后单击Toggle method Breakpoint创建方法断点。

#3


6  

You can also set breakpoints on specific exceptions. From the Debug perspective, there's a button "Add Java Exception Breakpoint", and there you can add "NullPointerException". Your debugger will then suspend execution as soon as such an exception is raised.

您还可以对特定的异常设置断点。从调试的角度来看,有一个按钮“添加Java异常断点”,在那里您可以添加“NullPointerException”。一旦出现这种异常,调试器就会暂停执行。

#4


1  

Normally, you should be able to set a break point. Especially if the 3rd party library is open source. But if your 3rd party lib is from a commercial vendor, they may have compiled the source with the debug flag turned off. This will make it impossible for you to debug into it. Your vendor might have done this as part of an obfuscation process to make it impossible to reverse engineer the library, or just simply because the final compiled classes will be smaller.

通常,您应该能够设置断点。特别是如果第三方库是开源的。但是,如果您的第三方lib来自于一个商业供应商,那么他们可能已经使用调试标志来编译源代码。这将使您无法调试它。您的供应商可能将此作为混淆过程的一部分,以使逆向工程库变得不可能,或者仅仅因为最终编译的类会更小。

#5


1  

Just attach the source (or use something that automatically attaches the source jar) and then set a breakpoint in the normal way, by double-clicking to the left of the line of interest.

只需附加源(或者使用自动附加源jar的东西),然后以常规方式设置断点,方法是双击感兴趣的行左边。

#6


1  

To make this work with a maven materialized web app, I had to do three things.

要使用maven物化web应用程序完成这项工作,我必须做三件事。

1) Create a new eclipse project with the source code of the 3rd party jar.

1)使用第三方jar的源代码创建一个新的eclipse项目。

2) Remove the reference to the jar from pom.xml dependencies.

2)从pom中删除对jar的引用。xml的依赖关系。

3) Add the new eclipse project to Deployment Assembly in the project properties.

3)将新的eclipse项目添加到项目属性中的部署程序集。

4) Add the new eclipse project to project Properties -> Java Build Path -> Projects of the existing project that references the 3rd party project.

4)将新的eclipse项目添加到项目属性——> Java构建路径——现有项目中引用第三方项目的>项目。

If the 3rd project has been added to your maven repository correctly, with sources, maven will automagically download the appropriate source code and let you add breakpoints without you having to do any of the steps above; however, I have learnt that you can't always count on this.

如果第3个项目已经正确地添加到maven存储库中,使用源代码,maven将自动下载适当的源代码,并允许您添加断点,而无需执行上述任何步骤;然而,我明白你不能总是指望这一点。

#1


32  

The most sure-fire way to do this (and end up with something that's actually useful) is to download the source (you say that it is open-source), and set up another "Java Project" pointing at that source.

最可靠的方法是下载源代码(您说它是开源的),并设置另一个指向该源的“Java项目”。

To do that, get the source downloaded and unzipped somewhere on your system. Click "File"->"New"->"Java Project". In the next dialog, give it a project name and select "Create Project from Existing Source". Browse to the root location of the open source library.

要做到这一点,请将源代码下载并解压到系统的某个位置。点击“文件”- >“新”- >“Java项目”。在下一个对话框中,给它一个项目名称并选择“从现有的源创建项目”。浏览到开源库的根位置。

Supposing that all the additional libraries that are required by the project and such are included in the project you downloaded, Eclipse will figure everything out and set the build path up for you.

假设项目所需的所有附加库都包含在您下载的项目中,Eclipse将为您确定一切并设置构建路径。

You'll need to remove the open source jar from your project's build path, and add this new project to the build path of your project.

您将需要从项目的构建路径中删除开放源码jar,并将这个新项目添加到项目的构建路径中。

Now, you can just treat this as your code, and debug at will.

现在,您可以将此作为您的代码,并随意调试。

This gets around at least a couple of problems with other approaches:

这至少解决了其他方法的几个问题:

  1. You could "attach source" to the jar file, but if the jar file was compiled without debug information, this still won't work. If the jar file was compiled with debug information (lines,source,vars...see http://java.sun.com/j2se/1.3/docs/tooldocs/win32/javac.html, and the -g option).

    您可以“附加源代码”到jar文件,但是如果没有调试信息编译jar文件,这仍然不能工作。如果用调试信息编译jar文件(行、源、vars…)请参阅http://java.sun.com/j2se/1.3/docs/tooldocs/win32/javac.html,以及-g选项)。

  2. You could add an "exception breakpoint" to see when the NullPointerException is raised, but that's a common exception, and may well get raised and dealt with many (hundreds of?) times prior to the one you're looking for. Plus, without the original source, you won't be able to really see much of anything about the code that is throwing the NullPointerException - the likelihood you'll be able to figure out what's wrong is pretty low.

    您可以添加一个“异常断点”,以查看何时引发NullPointerException,但这是一个常见的异常,并且很可能在您正在查找的异常之前被多次(数百次?)提出和处理。另外,如果没有原始的源代码,您将无法真正看到抛出NullPointerException的代码的任何内容—您能够找出错误的可能性非常低。

#2


59  

You can easily set method breakpoints in 3rd party libraries without having the source. Just open the class (you'll get the "i-have-no-source" view). Open the outline, right-click on the method you want and click on Toggle Method Breakpoint to create the method breakpoint.

您可以轻松地在第三方库中设置方法断点,而不需要源代码。打开这个类(你会看到“i-have-no-source”视图)。打开outline,右键单击您想要的方法,然后单击Toggle method Breakpoint创建方法断点。

#3


6  

You can also set breakpoints on specific exceptions. From the Debug perspective, there's a button "Add Java Exception Breakpoint", and there you can add "NullPointerException". Your debugger will then suspend execution as soon as such an exception is raised.

您还可以对特定的异常设置断点。从调试的角度来看,有一个按钮“添加Java异常断点”,在那里您可以添加“NullPointerException”。一旦出现这种异常,调试器就会暂停执行。

#4


1  

Normally, you should be able to set a break point. Especially if the 3rd party library is open source. But if your 3rd party lib is from a commercial vendor, they may have compiled the source with the debug flag turned off. This will make it impossible for you to debug into it. Your vendor might have done this as part of an obfuscation process to make it impossible to reverse engineer the library, or just simply because the final compiled classes will be smaller.

通常,您应该能够设置断点。特别是如果第三方库是开源的。但是,如果您的第三方lib来自于一个商业供应商,那么他们可能已经使用调试标志来编译源代码。这将使您无法调试它。您的供应商可能将此作为混淆过程的一部分,以使逆向工程库变得不可能,或者仅仅因为最终编译的类会更小。

#5


1  

Just attach the source (or use something that automatically attaches the source jar) and then set a breakpoint in the normal way, by double-clicking to the left of the line of interest.

只需附加源(或者使用自动附加源jar的东西),然后以常规方式设置断点,方法是双击感兴趣的行左边。

#6


1  

To make this work with a maven materialized web app, I had to do three things.

要使用maven物化web应用程序完成这项工作,我必须做三件事。

1) Create a new eclipse project with the source code of the 3rd party jar.

1)使用第三方jar的源代码创建一个新的eclipse项目。

2) Remove the reference to the jar from pom.xml dependencies.

2)从pom中删除对jar的引用。xml的依赖关系。

3) Add the new eclipse project to Deployment Assembly in the project properties.

3)将新的eclipse项目添加到项目属性中的部署程序集。

4) Add the new eclipse project to project Properties -> Java Build Path -> Projects of the existing project that references the 3rd party project.

4)将新的eclipse项目添加到项目属性——> Java构建路径——现有项目中引用第三方项目的>项目。

If the 3rd project has been added to your maven repository correctly, with sources, maven will automagically download the appropriate source code and let you add breakpoints without you having to do any of the steps above; however, I have learnt that you can't always count on this.

如果第3个项目已经正确地添加到maven存储库中,使用源代码,maven将自动下载适当的源代码,并允许您添加断点,而无需执行上述任何步骤;然而,我明白你不能总是指望这一点。