Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 820
Miguel Avatar asked Apr 21 '11 16:04

Miguel


People also ask

What does the javac command do?

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.

What is javac option?

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.

Which command line do you use to tell the javac compiler?

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.

What happens when you compile a Java program with the javac command or an IDE?

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.


2 Answers

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.

like image 67
ataylor Avatar answered Sep 19 '22 06:09

ataylor


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.

like image 25
karakuricoder Avatar answered Sep 19 '22 06:09

karakuricoder