setting debug-flag of javac

How to check, if a java-class was compiled with debug-options:

javap -l <Classname without .class>

Javap will show up the linenumbers and local variable tables (if present in class)… if not, you can be sure to know why your debugging-activities won’t work :-/

Here’s a small ant-script for demonstrating the setting of debug-option:

<project name="builder" basedir="." default="showJavapL">
	
	<!--call it with ant -f build.xml Ddebug=<off|on> and look at the output of "javap -l" -->

	<property name="src.dir" value="${basedir}/src" />
	<property name="target.dir" value="${basedir}/target" />
	<property name="classes.dir" value="${target.dir}/classes" />

	<target name="clean">
		<delete dir="${target.dir}" />
	</target>

	<target name="prepare" depends="clean">
		<mkdir dir="${target.dir}" />
		<mkdir dir="${classes.dir}" />
	</target>

	<target name="compile" depends="prepare">
		<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="${debug}" />
	</target>

	<target name="showJavapL" depends="compile">
		<exec executable="javap" dir="${basedir}">
			<arg value="-l" />
			<arg value="target.classes.org.jtaddeus.example.Counter" />
		  </exec>
	</target>

</project>

call it and look at the output:

Buildfile: C:\dev-env-fxp\eclipse-workspace\debugging-info-test\build.xml

clean:
   [delete] Deleting directory C:\dev-env-fxp\eclipse-workspace\debugging-info-test\target

prepare:
    [mkdir] Created dir: C:\dev-env-fxp\eclipse-workspace\debugging-info-test\target
    [mkdir] Created dir: C:\dev-env-fxp\eclipse-workspace\debugging-info-test\target\classes

compile:
    [javac] C:\dev-env-fxp\eclipse-workspace\debugging-info-test\build.xml:19: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to fal
se for repeatable builds
    [javac] Compiling 1 source file to C:\dev-env-fxp\eclipse-workspace\debugging-info-test\target\classes

showJavapL:
     [exec] public class org.jtaddeus.example.Counter extends java.lang.Object{
     [exec] public org.jtaddeus.example.Counter(int, int);
     [exec]
     [exec]
     [exec]
     [exec] public void echo();
     [exec]
     [exec]
     [exec]
     [exec] }
     [exec]

BUILD SUCCESSFUL
Total time: 1 second
Buildfile: C:\dev-env-fxp\eclipse-workspace\debugging-info-test\build.xml

clean:
   [delete] Deleting directory C:\dev-env-fxp\eclipse-workspace\debugging-info-test\target

prepare:
    [mkdir] Created dir: C:\dev-env-fxp\eclipse-workspace\debugging-info-test\target
    [mkdir] Created dir: C:\dev-env-fxp\eclipse-workspace\debugging-info-test\target\classes

compile:
    [javac] C:\dev-env-fxp\eclipse-workspace\debugging-info-test\build.xml:19: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to fal
se for repeatable builds
    [javac] Compiling 1 source file to C:\dev-env-fxp\eclipse-workspace\debugging-info-test\target\classes

showJavapL:
     [exec] Compiled from "Counter.java"
     [exec] public class org.jtaddeus.example.Counter extends java.lang.Object{
     [exec] public org.jtaddeus.example.C
     [exec] ounter(int, int);
     [exec]   LineNumberTable:
     [exec]    line 18: 0
     [exec]    line 19: 4
     [exec]    line 20: 9
     [exec]    line 21: 14
     [exec]
     [exec]   LocalVariableTable:
     [exec]    Start  Length  Slot  Name   Signature
     [exec]    0      15      0    this       Lorg/jtaddeus/example/Counter;
     [exec]    0      15      1    a       I
     [exec]    0      15      2    b       I
     [exec]
     [exec]
     [exec] public void echo();
     [exec]   LineNumberTable:
     [exec]    line 24: 0
     [exec]    line 25: 13
     [exec]    line 24: 24
     [exec]    line 27: 30
     [exec]
     [exec]   LocalVariableTable:
     [exec]    Start  Length  Slot  Name   Signature
     [exec]    5      25      1    i       I
     [exec]    0      31      0    this       Lorg/jtaddeus/example/Counter;
     [exec]
     [exec]
     [exec] }
     [exec]

BUILD SUCCESSFUL
Total time: 1 second