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) {
<!-- build.xml -->
<project name="TestProject" default="build" basedir=".">
 <target name="build">
  <javac srcdir="." destdir="."/>
[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" />

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.
Thanks. Very helpful.
thanks for this tipp...saved my day today

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! saved my life!
Thanks :D
thank you!
Top tip. Many thanks for that.
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 a lot , I appreciate your help

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 Nilesh and ota.
Thanks for the explanation.
At this very moment in time....you are like GOD to me!
3 years.. and still saving lives =) Kudos on the post. (And shame on the ant team for not making debug="true" the default)
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
Oes Tsetnoc one of the ways in which we can learn seo besides Mengembalikan Jati Diri Bangsa. By participating in the Oes Tsetnoc or Mengembalikan Jati Diri Bangsa we can improve our seo skills. To find more information about Oest Tsetnoc please visit my Oes Tsetnoc pages. And to find more information about Mengembalikan Jati Diri Bangsa please visit my Mengembalikan Jati Diri Bangsa pages. Thank you So much.
Oes Tsetnoc | Semangat Mengembalikan Jati Diri Bangsa
thanks a lot!
Thanks. Very nicely explained. Keep it up....
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. 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?