简体   繁体   English

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

[英]How to set a breakpoint in Eclipse in a third party library?

I'm getting a NullPointerException in a Class from a 3rd party library.我在来自第 3 方库的类中收到 NullPointerException。 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.但在我看来,我无法在第 3 方的类中设置断点。

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.更新:该库是开源的。

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. 打开大纲,右键单击所需的方法,然后单击“ Toggle Method Breakpoint以创建方法断点。

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". 单击“文件” - >“新建” - >“Java项目”。 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. 浏览到开源库的根位置。

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. 你可以“附加源代码”到jar文件,但如果jar文件是在没有调试信息的情况下编译的,那么这仍然无效。 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文件是使用调试信息编译的( lines,source,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. 您可以添加一个“异常断点”来查看何时引发NullPointerException,但这是一个常见的异常,并且很可能会在您正在寻找之前提升并处理许多(数百?)次。 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的代码的任何内容 - 您将能够找出错误的可能性非常低。

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". 从Debug透视图中,有一个“添加Java异常断点”按钮,您可以在其中添加“NullPointerException”。 Your debugger will then suspend execution as soon as such an exception is raised. 一旦引发此类异常,您的调试器就会暂停执行。

只需附加源(或使用自动附加源jar的东西)然后通过双击感兴趣的行的左侧以正常方式设置断点。

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.xml依赖项中删除对jar的引用。

3) Add the new eclipse project to Deployment Assembly in the project properties. 3)在项目属性中将新的eclipse项目添加到Deployment Assembly。

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; 如果第3个项目已经正确地添加到您的maven存储库中,那么maven将自动下载相应的源代码,并允许您添加断点,而无需执行上述任何步骤; however, I have learnt that you can't always count on this. 但是,我了解到你不能总是指望这一点。

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. 但是,如果您的第三方lib来自商业供应商,他们可能已经关闭了调试标志来编译源。 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. 您的供应商可能已将此作为混淆过程的一部分,以使其无法对库进行反向工程,或仅仅因为最终编译的类将更小。

In maven projects, easily you can open maven dependencies tree view in project explorer view and see a list of jar files that your project depends on.在 Maven 项目中,您可以轻松地在项目资源管理器视图中打开 Maven 依赖关系树视图,并查看项目所依赖的 jar 文件列表。 Open project explorer and then expand your project.打开项目资源管理器,然后展开您的项目。 Then, look for maven dependencies and expand this tree view.然后,查找 maven 依赖项并展开此树视图。 Now, you can expand any jar file and see list of available classes and by pressing enter on any of them, Eclipse will decompile the source for you and you can set a breakpoint.现在,您可以展开任何 jar 文件并查看可用类的列表,并在其中任何一个上按 Enter 键,Eclipse 将为您反编译源代码,您可以设置断点。 If your project has sub maven module projects, you should look for dependencies in your modules.如果你的项目有子 maven 模块项目,你应该在你的模块中查找依赖项。 I recommend to install Enhance class decompiler plugin , So you can easily debug your project without source code or you can specify source for eclipse to avoid source not found while debugging.If the third party jar file's source is already open as project in eclipse, You can not expand it in Maven dependencies section.我推荐安装Enhance class decompiler plugin ,这样你就可以不用源代码轻松调试你的项目,或者你可以为eclipse指定源以避免调试时找不到源。如果第三方jar文件的源已经在eclipse中作为项目打开,你无法在 Maven 依赖项部分展开它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM