Java Eclipse调试“找不到源”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1960158/
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-13 01:49:02  来源:igfitidea点击:

Eclipse debugging "source not found"

javaeclipsedebugging

提问by James

I just started using Eclipse so go easy on me ;). But when trying to debug a JUnit test case I get a dialog that states the the source is not found when I get to this line in the code in my test method:

我刚开始使用 Eclipse,所以请放轻松;)。但是当尝试调试 JUnit 测试用例时,我得到一个对话框,指出当我在测试方法的代码中到达这一行时找不到源:

Assert.assertEquals(1, contents.size());

I know I should probably go and try and download the source from somewhere, but I really don't want to because I have no interest in stepping into the JUnit code. I have the JUnit runtime jar so Why does Eclipse expect me to have all the referenced tools source code in order to debug my own code (seems somewhat silly)?

我知道我可能应该去尝试从某个地方下载源代码,但我真的不想这样做,因为我对进入 JUnit 代码没有兴趣。我有 JUnit 运行时 jar,那么为什么 Eclipse 要求我拥有所有引用的工具源代码以调试我自己的代码(似乎有些愚蠢)?

My main question is though, how can I tell Eclipse to skip this dialog when the source is not available and allow me to continue to debug my own code?

我的主要问题是,当源不可用时,如何告诉 Eclipse 跳过此对话框并允许我继续调试自己的代码?

[Edit]

[编辑]

I've isolated the cause of this. It seems that Eclipse seems to think it needs the source when an exception is thrown by the internal JUnit code. In general is there anyway to tell it that it doesn't and just have it throw up an error dialog of some kind instead?

我已经隔离了这个原因。当内部 JUnit 代码抛出异常时,Eclipse 似乎认为它需要源。一般来说,无论如何要告诉它它没有,而只是让它抛出某种错误对话框?

采纳答案by Chris Dutrow

I had this very annoying problem for a long time but was finally able to solve it. In my case, a null pointer exception was being thrown somewhere in Java's Transformer.IsRuntimeCode(ProtectionDomain) function.

我有这个非常烦人的问题很长一段时间,但终于能够解决它。就我而言,在 Java 的Transformer.IsRuntimeCode(ProtectionDomain) 函数中的某处抛出了一个空指针异常。

I didn't really need to know about this since the exception was being caught and handled, but eclipse would pause debugging every time this happened and tell me that the source wasn't available. As a result, I constantly had to keep pressing the button to continue code execution.

我真的不需要知道这一点,因为异常被捕获和处理,但是每次发生这种情况时 eclipse 都会暂停调试并告诉我源不可用。结果,我不得不不断按下按钮才能继续执行代码。

In order to prevent this from happening, I:

为了防止这种情况发生,我:

  1. Clicked on the "Breakpoints" window at the bottom of the debugging screen
  2. Right clicked "NullPointerException"
  3. Unchecked "Caught"
  1. 单击调试屏幕底部的“断点”窗口
  2. 右键单击“NullPointerException”
  3. 未选中的“捕获”

This prevented the debugger from pausing program flow during a caught NullPointerException.

这阻止了调试器在捕获 NullPointerException 期间暂停程序流。

alt text
(source: SharpDetail.com)

替代文字
(来源:SharpDetail.com

回答by Thorbj?rn Ravn Andersen

Calculate contents.size() on a separate line instead or set a breakpoint on the method.

改为在单独的行上计算 contents.size() 或在方法上设置断点。

Also note the junit view in Eclipse allows you to navigate the stack trace.

另请注意 Eclipse 中的 junit 视图允许您导航堆栈跟踪。

回答by VonC

The debug callstack will display a JUnit source code line when throwing an exception.
But you should not need to worry about that, if you do not have the source code of JUnit.

抛出异常时,调试调用堆栈将显示 JUnit 源代码行。
但是,如果您没有 JUnit 的源代码,则不必担心。

If you go back one line in the callstack, you should see the line (of yoursource code) which has caused the JUnit exception.
That should be enough to debug your code.

如果你回到一行在调用堆栈,你应该可以看到(的线曾引起了JUnit异常源代码)。
这应该足以调试您的代码。



To associate the source with JUnit, you could add the junit.jarin the librairies of your project, and associates the junit-x.y.z-src.jarto the junit-x.y.z.jar, like so:

使用JUnit源相关联,您可以添加junit.jar在你的项目的librairies,以及相关联的junit-x.y.z-src.jarjunit-x.y.z.jar像这样

http://web.archive.org/web/20130227201940/http://img241.i_mageshack.us/img241/1412/eclipsejunitsrc.png

http://web.archive.org/web/20130227201940/http://img241.i_mageshack.us/img241/1412/eclipsejunitsrc.png

That will generate in the .classpathof your project a line like:

这将在.classpath您的项目中生成如下一行:

<classpathentry kind="lib" path="junit-x.y.z.jar" sourcepath="junit-x.y.z-src.jar">

Note: actually, there would be the full path of the junit[...].jarfiles in this classpathentryline. But you could also use Linked resourcesto avoid that fixed value (the full path) in your .classpathfile.

注意:实际上,junit[...].jar这一classpathentry行中会有文件的完整路径。但是您也可以使用链接资源来避免文件中的固定值(完整路径).classpath

回答by user1370258

I had a similar problem. I fixed it by right clicking on the project folder in the package explorer and selecting refresh. The code source was out of sync with the debugger and this corrected it. The Transformer.IsRuntimeCode(ProtectionDomain) Source not found message no longer appears.

我有一个类似的问题。我通过右键单击包资源管理器中的项目文件夹并选择刷新来修复它。代码源与调试器不同步,这更正了它。不再出现 Transformer.IsRuntimeCode(ProtectionDomain) Source not found 消息。

回答by Mashimom

I had a similar problem with another jar, even when I pointed to the source it would ask for it again. I was able to solve it by compiling the jar with debug="on"on ANT.

我在另一个 jar 中遇到了类似的问题,即使我指向源,它也会再次要求它。我能够通过debug="on"在 ANT 上编译 jar 来解决它。

回答by Steve Pitchers

Use the Step Filter to avoid stepping through the ...junit... packages. Right click on the stack trace and choose Filter Package. You may have to turn on filtering first with Use Step Filters. ~~~

使用 Step Filter 避免单步执行 ...junit... 包。右键单击堆栈跟踪并选择过滤器包。您可能必须先使用“使用步进过滤器”打开过滤。~~~