What does the javac debugging information option -g:vars do?

The -g:vars option will insert a LocalVariableTable into your class file. For example, with this test class:

public class Test {
    public static void main(String[] args) {
        int mylocal = 1;
        System.out.println("" + mylocal);
    }
}

You can take a look at the debugging information in the class file with javap -l Test. With no -g arguments, there is just a LineNumberTable. This is what the JVM uses to generate the line numbers you see in stacktraces. If you compile with -g:vars, you’ll notice there is now a LocalVariableTable that looks like this:

LocalVariableTable: 
 Start  Length  Slot  Name   Signature
 0      3      0    args       [Ljava/lang/String;
 2      1      1    mylocal       I

This captures the name and type of each parameter and local variable by its position on the stack.

You don’t normally need this for debugging if you have the source available. However if you don’t have the source it can be useful. For example, run jdb Test with and without -g:vars:

Initializing jdb...
> stop in Test.main
Deferring breakpoint Test.main.
It will be set after the class is loaded.
> run
main[1] next
main[1] next
main[1] locals
Method arguments:
args = instance of java.lang.String[0] (id=354)
Local variables:
mylocal = 1

You’ll only get the list of locals if the class was compiled with -g:vars.

Leave a Comment