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:
- -g:source
- Include the Java™ source file name in the compiled class file.
- -g:lines
- 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.
- -g:vars
- 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.
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(Book.java:16)
at com.example.Author.getBookTitles(Author.java:25)
at com.example.Bootstrap.main(Bootstrap.java:14)
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.