How to make Java compiler generate line numbers in compiled code

*爱你&永不变心* 提交于 2019-12-23 18:19:55

问题


I have a hello world program called A.class. It was compiled using command javac A.java. All it does is print "hello world".

Next, I compiled using javac -g A.java. I am expecting to see line numbers, but can't see. Any idea what happened?

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.

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.


回答1:


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.

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 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).



来源:https://stackoverflow.com/questions/43376559/how-to-make-java-compiler-generate-line-numbers-in-compiled-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!