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

前端 未结 6 1672
无人及你
无人及你 2020-12-01 00:08

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.

相关标签:
6条回答
  • 2020-12-01 00:18

    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.

    0 讨论(0)
  • 2020-12-01 00:28

    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.

    0 讨论(0)
  • 2020-12-01 00:35

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

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

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

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

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

    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.

    0 讨论(0)
  • 2020-12-01 00:36

    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.

    0 讨论(0)
  • 2020-12-01 00:37

    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.

    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.

    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.

    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.

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

    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.

    0 讨论(0)
  • 2020-12-01 00:44

    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.

    0 讨论(0)
提交回复
热议问题