Compiling Java for optimal debugging

The type and amount of debug information included within a compiled .class file depends upon the defaults of the build tool and the explicitly specified debug options.

Debug information options in IBM Java

These are the relevant debug options for the IBM® javac compiler:

Include the Java™ source file name in the compiled class file.
Include a LineNumberTable in the class file. This table maps each source file line number to the generated java bytecode instructions; the machine code statements used by the Java Virtual Machine.
Include a LocalVariableTable for each non-native and non-abstract method in the class file. This table includes the name and type of each local variable and the scope in which the variable is valid.
Important: If there is any likelihood that debugging of Java programs will be required, use the following option to include all debug information in all compiled .class files.
javac -g:source,lines,vars <class files>

The result of this option is that any stack traces that appear because of exceptions will include location information of each stack entry. For example:

Exception in thread "main" java.lang.NullPointerException
        at com.example.Book.getTitle(
        at com.example.Author.getBookTitles(
        at com.example.Bootstrap.main(

If the debug options are not specified or the -g:none option is used, much less information is available for problem determination:

Exception in thread "main" java.lang.NullPointerException
        at com.example.Book.getTitle(Unknown location)
        at com.example.Author.getBookTitles(Unknown location)
        at com.example.Bootstrap.main(Unknown location)

Not only does this reduce the value of stack traces, it prevents the names of local variables from appearing when debugging programs. Instead, all variables simply appear as "<local variable>" or a similarly unhelpful name, depending on the development environment.