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)

Comments:
Thanks.

From Java exception - unknown source - why???, I learned that setting "debug=true" is sufficient for this.
 
Thanks. Very helpful.
 
thanks for this tipp...saved my day today
 
thanks!

btw, you rock! :)
 
Thanks Nilesh.

Your tip saved a lot of time.
 
Great tip! Thanks very much.
 
Thanks, that makes life a lot easier, strange default!
 
thx for the info.
 
Thanks for most useful TIP. It saves lot of time.
 
This problem has cost me a lot of time over the years. I just thought that's the way it is.

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
 
thanks! saved my life!
 
Thanks :D
 
thnks
 
thank you!
 
Top tip. Many thanks for that.
 
Dhanyavaad!
 
Many Thanks. You save other developer's time.
 
Thanks! That's something I was searching for years!!! Greetings from Austria.
 
Excellent,You saved my days too.
Thanks
Ramkumar
 
Thanks a lot , I appreciate your help

--Vinay--
 
Thanks it was very helpful..Keep posting such useful tips you come across..

- Mathew
 
Thanx...saved me :)
 
Thanks so much. It was a life saver!
 
Thanks
 
Thanks Nilesh and ota.
 
Thanks for the explanation.
 
At this very moment in time....you are like GOD to me!
 
Thanks it helped me
 
Great blog as for me. I'd like to read something more concerning that topic.
BTW look at the design I've made myself Overnight escorts
 
thanks a lot!
 
Thanks. Very nicely explained. Keep it up....
 
thanks.
 
thanks, saved my day
 
Gracias, ete error lo tuve port muuuucho tiempo, ahora estoy OK
 
Thanksa lot dear I spent 3 days on my issue.

After reading this solved in 3 min.

Thanks.
 
Thanks. It was useful.
 
Thanks. It solved my problem precisely.
 
Thanks you much, you have saved my time, I was facing exactly same problem.

When I search in google your blog reflected on Top. with key word
JAVA error stack trace Unknown Source
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?