The topic of this Issue Blog is in regards to getting an “Unknown Source” in place of a line number in a Java exception stack trace when being compiled by Apache Ant. I’ve come across this issue several times in the past but have never really given it much thought because I can usually pin point my errors through debug logs when I have to. It happened to me twice recently that I had to debug something that led me back to this “Unknown Source” in my stack trace so I thought I’d Issue Blog It for safe keeping.
The Background
When compiling Java code with Apache Ant occasionally “Unknown Source” is displayed rather than the line number of the compilation error in the stack trace. This sparked my curiosity as the line numbers would report back correctly when being compiled by Java and the Eclipse IDE.
The Problem
The exception stack trace displays “Unknown Source” instead of the line number of the compilation error when being built by an Ant build script.
The Resolution
The resolution is actually very simple. There is a debug mode in Ant that, if not enabled, sends
-g:none
to the command line for compilers that support it. This basically specifies not to generate any debugging information.
To fix this simply add the following attributes to the javac task in the build.xml which is building the Java program.
debug="on" debuglevel="lines,vars,source"
This “turns on” the debugging information and sets the debug levels of lines, vars and source to display… hence “turning on” the line numbers in the stack trace.
I thought it important to note that the compiler will not simply add debug info to a class as the result of a subsequent build. The class must have changed since last build. FOund that the hard way!