4

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.

9
  • 4
    -g is for generating debug information. class files are bytecode, not human readable. You can use javap to disassemble a class file. Commented Apr 12, 2017 at 18:04
  • 1
    Have you actually pasted the contents of the .class file into the question? Have you really done that? And if I may ask, why have you done it twice? Commented Apr 12, 2017 at 18:08
  • First one is with javac. Second one is compiled with javac -g <<<. Notice the "g". No line number! Javap is great, but still no line number! Commented Apr 12, 2017 at 18:10
  • Uh. Wow. What are you trying to achieve? Forget about your solution and tell us why you would want this. Commented Apr 12, 2017 at 18:20
  • I want this to see if there is any kind of performance impact in maintaining line numbers, and secondly, I want to see how log4j (and other loggers) manage to obtain line number for logging. So I need this real bad. But I am not able to see line numbers in javac -g generated bytecode. Help! Commented Apr 12, 2017 at 18:24

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.