问题描述
我有一个名为 A.class 的hello世界程序.它是使用命令javac A.java
编译的.它所做的只是打印"hello world" .
I have a hello world program called A.class. It was compiled using command javac A.java
. All it does is print "hello world".
接下来,我使用javac -g A.java
进行编译.我期望看到行号,但看不到.知道发生了什么事吗?
Next, I compiled using javac -g A.java
. I am expecting to see line numbers, but can't see. Any idea what happened?
我确实看到在javac编译的.class文件和javac -g编译的某些特殊字符之间的细微差别.但是我看不到任何行号.
I do see very minor differences in some kind of special characters between .class file of javac compiled, and javac -g compiled. But I can't see any line numbers.
我对此的好奇心是因为我想找到行号可能会对性能产生什么样的影响.其次,我想知道log4j等如何维护记录的行号.谢谢.
My curiosity for this is because I want to find what kind of impact line numbers may have on performance. Second, I want to know how log4j etc maintain line numbers for logging. Thanks.
推荐答案
编译命令很好,-g
确实打开了调试信息的生成.顺便说一句:默认情况下会生成行号,需要-g:none
或类似的功能将其关闭.
The compile command is good, -g
turns on generation of debug information, indeed. BTW: line numbers are generated by default, need -g:none
or similar to turn this off.
缺少的是一种有效检查生成的.class
文件的方法,类似于工具使用它的方式.试试:
What's missing is a way to meaningfully inspect the generated .class
file, similar to how a tool would use it. Try:
$ javap -l -c A.class
-l
打开行号表的打印.-c
打开反汇编后的字节码指令的打印(由于行号表将源行号与字节码指令相关联,因此可能很有趣).
-l
turns on printing of line number tables.-c
turns on printing of disassembled bytecode instructions (might be interesting since line number tables relate source line numbers to bytecode instructions).
这篇关于如何使Java编译器在已编译的代码中生成行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!