java 确定 .class 文件是否使用调试信息编译?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1508235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine whether .class file was compiled with debug info?
提问by Atos
How can I determine for any Java .class file if that was compiled with debug info or not?
如何确定任何 Java .class 文件是否使用调试信息编译?
How can I tell exactly what -g{source|lines|vars} option was used?
我怎样才能确切地知道使用了什么 -g{source|lines|vars} 选项?
回答by Pete Kirkham
If you're on the command line, then javap -l will display LineNumberTable and LocalVariableTable if present:
如果您在命令行上,则 javap -l 将显示 LineNumberTable 和 LocalVariableTable(如果存在):
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 -cwill display the source file if present at the start of the decompilation:
javap -c如果在反编译开始时存在,将显示源文件:
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 ASMrather than writing yet another bytecode reader.
以编程方式,我会查看ASM而不是编写另一个字节码阅读器。
回答by Aaron Digulla
You must check the Codestructurein the class file and look for LineNumberTableand LocalVariableTableattributes.
您必须检查类文件中的Code结构并查找LineNumberTable和LocalVariableTable属性。
Tools like ASMor Apache Commons BCEL (Byte Code Engineering Library) will help: https://commons.apache.org/proper/commons-bcel/apidocs/index.html?org/apache/bcel/classfile/LineNumberTable.html
像ASM或 Apache Commons BCEL(字节代码工程库)这样的工具会有所帮助:https://commons.apache.org/proper/commons-bcel/apidocs/index.html?org/apache/bcel/classfile/LineNumberTable.html

