Determine whether .class file was compiled with debug info?

后端 未结 3 1985
情话喂你
情话喂你 2020-12-04 15:44

How can I determine for any Java .class file if that was compiled with debug info or not?

How can I tell exactly what -g{source|lines|vars} option was used?

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 16:23

    If you're on the command line, then javap -l will display LineNumberTable and LocalVariableTable if present:

    peregrino:$ javac -d bin -g:none src/Relation.java 
    peregrino:$ javap -classpath bin -l Relation 
    public class Relation extends java.lang.Object{
    public Relation();
    
    peregrino:$ javac -d bin -g:lines src/Relation.java 
    peregrino:$ javap -classpath bin -l Relation 
    public class Relation extends java.lang.Object{
    public Relation();
      LineNumberTable: 
       line 1: 0
       line 33: 4
    
    peregrino:$ javac -d bin -g:vars src/Relation.java 
    peregrino:$ javap -classpath bin -l Relation 
    public class Relation extends java.lang.Object{
    public Relation();
    
      LocalVariableTable: 
       Start  Length  Slot  Name   Signature
       0      5      0    this       LRelation;
    

    javap -c will display the source file if present at the start of the decompilation:

    peregrino:$ javac -d bin -g:none src/Relation.java 
    peregrino:$ javap -classpath bin -l -c Relation | head
    public class Relation extends java.lang.Object{
      ...
    
    peregrino:$ javac -d bin -g:source src/Relation.java 
    peregrino:$ javap -classpath bin -l -c Relation | head
    Compiled from "Relation.java"
    public class Relation extends java.lang.Object{
      ...
    

    Programmatically, I'd look at ASM rather than writing yet another bytecode reader.

提交回复
热议问题