Unknown Source in Exception or Stacktrace of Java

UPDATED: 19 February 2015
Unknown Source in Exception or Stacktrace of Java

Its very rare situation when you face this issue in Java Programming. Possibly there are very few people have seen this error. This exception occur when you compile your Java code with no "debugging information" flag.

Why such provision in Java?
This option given by Java to prevent hacking. When you compile your code "without debugging information" then it won't show line number in exception instead it shows "Unknown Source".


Solution For any Integrated Development Environment (IDE)
I've not tested this setting with any IDE but find the code compilation settings in your IDE and set flag as per your requirement.

-g ~ This will compile code with debugging information [Shows line number in exception]
-g:none ~ This will compile code without debugging information [Won't show line number/Unknown Source]


Source Code
/**
 * ------------------------------------------
 * This will shows line number in exception
 * ------------------------------------------
 * javac -g UnknownSourceExample.java 
 * java UnknownSourceExample
 * 
 * ------------------------------------------
 * This won't shows line number in exception
 * ------------------------------------------
 * javac -g:none UnknownSourceExample.java 
 * java UnknownSourceExample
 */
public class UnknownSourceExample{
 public static void main(String args[]){
  System.out.println(10/0);
 }
}

Output
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at UnknownSourceExample.main(UnknownSourceExample.java:3)
------------------------------------------  
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at UnknownSourceExample.main(Unknown Source)

Jenkins / ANT Compilation
We've just setup Jenkins for scheduled war compilation and on exception we face UnknownSource issue. We are still in learning phase and didn't know that we've to set debug attribute in build.xml of ANT. If you are also facing same issue then here is the solution that may help you.

Find code compilation tag <javac> in your build.xml, add debug and debuglevel attribute in it and now build your project through Jenkins so it'll show line number in exception instead Unknown Source. Your tag will look like as follow...
<javac debug="true" debuglevel="lines,vars,source" destdir="bin" srcdir="src"/>

0 comments :