java 如何在 ant build 创建的 jar 的错误堆栈跟踪中获取源行号?

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

How can I get the source line number in error stack trace of a jar created by ant build?

javaantjarjavac

提问by Ranger

I am using ant to build a jar of my project in eclipse. I deploy that jar on tomcat. But whenever an exception happen in my code (which is inside jar), the error stack trace comes but the line number does not come -- instead it says unknown source.

我正在使用 ant 在 eclipse 中构建我的项目的 jar。我在 tomcat 上部署了那个 jar。但是每当我的代码(在 jar 内)中发生异常时,错误堆栈跟踪就会出现,但行号没有出现——而是说来源未知。

How can I get the line numbers in error stack trace?

如何获取错误堆栈跟踪中的行号?

回答by Itay Maman

You need to compile your jar with debug information. Specifically, you need to find the javactask that compiles the classes that you later jar and add a debug="on"attribute. Example:

您需要使用调试信息编译您的 jar。具体来说,您需要找到javac编译您稍后 jar 的类并添加debug="on"属性的任务。例子:

<javac srcdir="${src}"
     destdir="${build}"
     classpath="xyz.jar"
     debug="on"
     source="1.4" />

Full details can be found here.

可以在此处找到完整的详细信息。

回答by Peter

The attribute "debug" requires values of "true" or "false" and is translated to javac -g option.
When explicitly wanting to specify a argument to -g, you can do that by defining the
attribute "debuglevel" , which accepts "source", "vars" and other values (see ant task
documentation for more details).
Setting debug="true" and debuglevel="source" will attach the source but won't provide line
number informations, debuglevel="lines,vars,source" will give you the information
that you need.

属性“debug”需要“true”或“false”的值并转换为 javac -g 选项。
当明确想要为 -g 指定参数时,您可以通过定义
属性 "debuglevel"来实现,该属性接受 "source"、"vars" 和其他值(
有关更多详细信息,请参阅 ant 任务文档)。
设置 debug="true" 和 debuglevel="source" 将附加源但不会提供行
号信息, debuglevel="lines,vars,source" 将为您提供所需的信息