Close

Compiling Java code with debugging information

[Last Updated: May 20, 2018]

Java Tools & Commands Java 

Use -g option of javac to include debug information with compiled code.

Debug information is useful if we don't have source code that we want to debug during runtime.

By default, only line number and source file information is generated i.e. without -g option is being specified.

Specifying -g includes local variable debugging information as well. We can see the real name of variables during debugging time.

We can also control what debug information should be included during compilation time by using -g:[keyword list] where keyword list is comma separated list of following keywords:

  • source: Source file debugging information.
  • lines: Line number debugging information.
  • vars: Local variable debugging information.


-g:none doesn't generate any debug information.


Is there any performance difference?

Debugging information is meta information only.

It increases the size of the class files, thus increasing load time.

During execution without debugging, this information is completely ignored. That means, there is no difference in performance if source compiled with debug information or not.


Examples

Let's compile this class with javac and then check the debugging information in class file using javap tool:

public class Test {
private String myStr;

public Test (String s) {
this.myStr = s;
}

public void print () {
System.out.println(myStr);
}

public static void main (String[] args) {
Test test = new Test("test");
test.print();
}
}


Compiling without -g option, no variable names will be included:


D:\examples>javac Test.java

D:\examples>javap -l Test
Compiled from "Test.java"
public class Test {
public Test(java.lang.String);
LineNumberTable:
line 4: 0
line 5: 4
line 6: 9

public void print();
LineNumberTable:
line 9: 0
line 10: 10

public static void main(java.lang.String[]);
LineNumberTable:
line 13: 0
line 14: 10
line 15: 14
}


Compiling with -g option, all debugging information will be included. Notice variable name tables:


D:\examples>javac -g Test.java

D:\examples>javap -l Test
Compiled from "Test.java"
public class Test {
public Test(java.lang.String);
LineNumberTable:
line 4: 0
line 5: 4
line 6: 9
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this LTest;
0 10 1 s Ljava/lang/String;

public void print();
LineNumberTable:
line 9: 0
line 10: 10
LocalVariableTable:
Start Length Slot Name Signature
0 11 0 this LTest;

public static void main(java.lang.String[]);
LineNumberTable:
line 13: 0
line 14: 10
line 15: 14
LocalVariableTable:
Start Length Slot Name Signature
0 15 0 args [Ljava/lang/String;
10 5 1 test LTest;
}


Compiling without any debugging information


D:\examples>javac -g:none Test.java

D:\examples>javap -l Test
public class Test {
public Test(java.lang.String);

public void print();

public static void main(java.lang.String[]);
}


Compiling with variable names only


D:\examples>javac -g:vars Test.java

D:\examples>javap -l Test
public class Test {
public Test(java.lang.String);
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this LTest;
0 10 1 s Ljava/lang/String;

public void print();
LocalVariableTable:
Start Length Slot Name Signature
0 11 0 this LTest;

public static void main(java.lang.String[]);
LocalVariableTable:
Start Length Slot Name Signature
0 15 0 args [Ljava/lang/String;
10 5 1 test LTest;
}


Compiling with line numbers and variables names:


D:\examples>javac -g:lines,vars Test.java

D:\examples>javap -l Test
public class Test {
public Test(java.lang.String);
LineNumberTable:
line 4: 0
line 5: 4
line 6: 9
LocalVariableTable:
Start Length Slot Name Signature
0 10 0 this LTest;
0 10 1 s Ljava/lang/String;

public void print();
LineNumberTable:
line 9: 0
line 10: 10
LocalVariableTable:
Start Length Slot Name Signature
0 11 0 this LTest;

public static void main(java.lang.String[]);
LineNumberTable:
line 13: 0
line 14: 10
line 15: 14
LocalVariableTable:
Start Length Slot Name Signature
0 15 0 args [Ljava/lang/String;
10 5 1 test LTest;
}



Note

The option -g does not include method parameters' names, for that we need to use javac's -parameters option.

See Also