如何让Java编译器在编译后的代码中生成行号 [英] How to make Java compiler generate line numbers in compiled code

查看:45
本文介绍了如何让Java编译器在编译后的代码中生成行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 A.class 的 hello world 程序.它是使用命令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.

推荐答案

compile 命令不错,-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编译器在编译后的代码中生成行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆