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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/370814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:56:00  来源:igfitidea点击:

How to set a breakpoint in Eclipse in a third party library?

javaeclipsedebuggingbreakpoints

提问by boutta

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.

我在来自第 3 方库的类中收到 NullPointerException。现在我想调试整个过程,我需要知道该类是从哪个对象中保存的。但在我看来,我无法在第 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.

更新:该库是开源的。

采纳答案by Jared

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 -goption).

  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.

  1. 您可以将源代码“附加”到 jar 文件,但是如果 jar 文件是在没有调试信息的情况下编译的,这仍然不起作用。如果 jar 文件是使用调试信息编译的(lines,source,vars...请参阅http://java.sun.com/j2se/1.3/docs/tooldocs/win32/javac.html-g选项)。

  2. 您可以添加一个“异常断点”来查看 NullPointerException 何时引发,但这是一个常见的异常,并且很可能在您要查找的异常之前引发和处理许多(数百?)次。另外,如果没有原始来源,您将无法真正看到有关抛出 NullPointerException 的代码的任何信息 - 您能够找出问题所在的可能性非常低。

回答by Joachim Sauer

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 Breakpointto create the method breakpoint.

您可以轻松地在第 3 方库中设置方法断点,而无需源代码。只需打开课程(您将看到“我没有资源”视图)。打开大纲,右键单击所需的方法,然后单击Toggle Method Breakpoint以创建方法断点。

回答by Bent André Solheim

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.

通常,您应该能够设置断点。特别是如果第 3 方库是开源的。但是,如果您的第 3 方库来自商业供应商,他们可能已经在关闭调试标志的情况下编译了源代码。这将使您无法对其进行调试。您的供应商可能已经这样做了,作为混淆过程的一部分,以使其无法对库进行逆向工程,或者仅仅是因为最终编译的类会更小。

回答by lindelof

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”。一旦出现此类异常,您的调试器就会暂停执行。

回答by Robin Green

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 的东西),然后通过双击感兴趣的行的左侧以正常方式设置断点。

回答by Ted Gulesserian

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)使用3rd方jar的源代码新建一个eclipse项目。

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

2) 从 pom.xml 依赖项中删除对 j​​ar 的引用。

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项目添加到引用3rd方项目的现有项目的项目属性->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.

如果第三个项目已正确添加到您的 maven 存储库中,带有源代码,maven 将自动下载相应的源代码并让您无需执行上述任何步骤即可添加断点;但是,我了解到您不能总是指望这一点。