23

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!

andersonbd1
  • 5,266
  • 14
  • 44
  • 62
  • Are you running the jvm in server mode, i.e. `java -server`? Also, are you running off of jre or a jdk installation? – shams Feb 09 '12 at 18:42
  • not using -server and I'm using a jdk – andersonbd1 Feb 09 '12 at 18:48
  • I never realized this could be set in javac in Ant... I was wondering why my stacktraces had gotten less useful lately. :P Changed now and life is better – Panky Oct 08 '12 at 20:44

4 Answers4

26

I think the correct way is:

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

Note there are no spaces between lines,vars,source

bvanvelsen - ACA Group
  • 1,741
  • 1
  • 14
  • 25
2

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

Community
  • 1
  • 1
shams
  • 3,460
  • 24
  • 24
  • I found that note as well, but I don't think applies for me. #1) I'm using a jdk and #2) I think that only matters because the compiled class files for the java api classes... however, this is a my own custom class – andersonbd1 Feb 09 '12 at 18:51
  • Anderson is correct, the jdk would only fill in debug info for sdk components. On top of which _the jre doesn't include a compiler_ . I think perhaps you shouldn't have posted this. – Justin Buser Jun 23 '12 at 22:39
1

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).

Franz
  • 119
  • 3
  • 8
-1
<property name="srcdir" location="src/"/>
<property name="builddir" location="build/"/>
<target name="compile">
  <exec executable="javac">
    <arg value="${srcdir}/*" />
    <arg value="-d" />
    <arg value="${builddir}"/>
  </exec>
</target>
  • 1
    Code-only answers are generally frowned upon on this site. Could you please edit your answer to include some comments or explanation of your code? Explanations should answer questions like: What does it do? How does it do it? Where does it go? How does it solve OP's problem? See: [How to anwser](https://stackoverflow.com/help/how-to-answer). Thanks! – Eduardo Baitello Oct 09 '19 at 17:36