What exactly the -g:vars
(local variable debugging information) option of javac provides as output.
Doing some tests, there is no addition information (example no difference between -g:source,lines and -g:source,lines,vars.
Does some one have an example of a these local variable debugging information?
Description. The javac command reads source files that contain module, package and type declarations written in the Java programming language, and compiles them into class files that run on the Java Virtual Machine. The javac command can also process annotations in Java source files and classes.
OPTIONS. Specifies the path javac uses to look up classes needed to run javac or being referenced by other classes you are compiling. Overrides the default or the CLASSPATH environment variable if it is set. Directories are separated by colons.
So make sure you included this directory in the PATH environment variable so it can be accessed anywhere in command line prompt. Type javac -help to view compiler options, and type javac -version to know current version of the compiler.
In Java, programs are not compiled into executable files; they are compiled into bytecode (as discussed earlier), which the JVM (Java Virtual Machine) then executes at runtime. Java source code is compiled into bytecode when we use the javac compiler.
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
.
From the javadocs:
-g Generate all debugging information, including local variables. By default, only line number and source file information is generated.
This does not produce visible output at compile time but is used in debugging during run time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With