Tuesday, August 08, 2006
java Exception - Unknown Source
When debugging my Java programs, reading through the exception stack trace I realized that many a times the line number in was not reported. Just the method name and file name with unknown source was reported. For example consider the following simple program compiled with build file as shown
//File: TestDebug.java public class TestDebug { public static void main(String[] args) { ((String)null).length(); } }
<!-- build.xml --> <project name="TestProject" default="build" basedir="."> <target name="build"> <javac srcdir="." destdir="."/> </target> </project>
[nilesh^trinity]$ ant build ... [nilesh^trinity]$ java TestDebug Exception in thread "main" java.lang.NullPointerException at TestDebug.main(Unknown Source)
After some investigation I found that ant omits debug information from compiled class by default. Hence producing exact line number in case of an error is not possible. The solution however is simple. I just modified my build file to turn debug on and to set debuglevel="lines,vars,source" under javac target in ant, and everything worked fine. Modified build file for above example will be:
<!-- build.xml --> <project name="TestProject" default="build" basedir="."> <target name="build"> <javac srcdir="." destdir="." debug="on" debuglevel="lines,vars,source" /> </target> </project>
Line numbers are now reported with the stack trace:
[nilesh^trinity]$ ant build ... [nilesh^trinity]$ java TestDebug Exception in thread "main" java.lang.NullPointerException at TestDebug.main(TestDebug.java:4)
From Java exception - unknown source - why???, I learned that setting "debug=true" is sufficient for this.
I notice that it's nearly two years since your original post. I wonder how many thousands of hours have been saved because of your effort.
Thanks
BTW look at the design I've made myself Overnight escorts
When I search in google your blog reflected on Top. with key word
JAVA error stack trace Unknown Source
<< Home