javac
has an option called -g
. This is an option to include debug information in the class file and vice versa.
-- -g: lines
will include the line number of the source code
-- -g: vars
will include the information of the local variable name
--Include the source file name with -g: source
-- -g
will include all debug information
-- -g: none
will not include any debug information
Suppose you have code like the following javac -g Hello.java
.
public class Hello {
public String say(String name) {
return "hello ".concat(name);
}
}
Then javap -l Hello
will do the following ( -l
is an option to output the row number and local variable table):
Compiled from "Hello.java"
public class Hello {
public Hello();
LineNumberTable:
line 1: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this LHello;
public java.lang.String say(java.lang.String);
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 7 0 this LHello;
0 7 1 name Ljava/lang/String;
}
The LineNumberTable
contains the line number, the LocalVariableTable
contains the local variable name, and the Compiled from ~
contains the source file name.
What if javac -g: none Hello.java
? Here is the javap -l Hello
.
public class Hello {
public Hello();
public java.lang.String say(java.lang.String);
}
It does not contain any debug information.
We usually do not explicitly javac
and compile using IDEs like Eclipse and IntelliJ IDEA, or Maven. However, you should be able to step through while looking at the variable names by opening the corresponding source code without worrying about it when debugging. That means that the IDE and Maven include debug information by default.
In Eclipse, you can set whether to include debug information. I tried to screenshot the default state. It now includes vars
, lines
, and source
.
I don't know because I don't use IntelliJ IDEA, but I think I can probably make similar settings.
Maven can also set whether to include debug information maven-compiler-plugin debug.
By the way, if you use javac Hello.java
, the source file name and line number will be included. In other words, it is the same as javac -g: source, lines Hello.java
by default.
To summarize the above,
--javac
includes debug information in class file with -g
option
--By default, the source file name and line number are included
--IDE and Maven will include local variable names in addition to source filenames and line numbers by default
It means that.