简体   繁体   中英

“Unknown Source” in java stack trace, yet line numbers are in the class file

I've written a super simple java class that is throwing exceptions as it should. However the stack trace I'm getting looks like this:

java.lang.RuntimeException: hello
        at Main.go(Unknown Source)
        at Main.main(Unknown Source)

Note: there are no line numbers in the stack trace and I would like there to be.

The answers you find when googling this problem are all about adding the correct parameters at compile time to make sure the line numbers actually make it into the class file. However, I don't believe that's my problem as I have this in my ant build.xml

<javac
  debug="true"
  debuglevel="lines,vars,source"
  includeAntRuntime="false"
  classpathref="classpath.compile"
  srcdir="${src.dir}"
  destdir="${build.classes}" />

Also, according to javap, it looks like the line numbers did make it in:

$ javap -l ./build/classes/Main | head -n 9
public class Main extends java.lang.Object{

public Main();
  LineNumberTable: 
   line 14: 0
   line 22: 4
   line 23: 15
   line 24: 26

So what gives? Is there a param I need to set in the jvm when I run the code?

Thanks!

I think the correct way is:

<javac debug="true" debuglevel="lines,vars,source"

Note there are no spaces between lines,vars,source

I had exactly the same problem. In our environment it helped to switch off the optimize-flag:

<javac optimize="off" ...

Apparently Ant does not ignore attribute optimize, although Ant-Doc says for Attribute "optimize" (and we are using Java 1.7):

Indicates whether source should be compiled with optimization; defaults to off. Note that this flag is just ignored by Sun's javac starting with JDK 1.3 (since compile-time optimization is unnecessary).

Found this answer on another question :

This is normally related to missing debug information. You are probably using JRE (not JDK), which does not include debug information for rt.jar classes. Try using full JDK, you'll get proper locations in the stack trace

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM