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.

Apurva Singh
  • 4,534
  • 4
  • 33
  • 42
  • 3
    -g is for generating debug information. class files are bytecode, not human readable. You can use javap to disassemble a class file. – OldProgrammer Apr 12 '17 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? – Mike Nakis Apr 12 '17 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! – Apurva Singh Apr 12 '17 at 18:10
  • Uh. Wow. What are you trying to achieve? Forget about your solution and tell us why you would want this. – Gray Apr 12 '17 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! – Apurva Singh Apr 12 '17 at 18:24
  • @ApurvaSingh It's better if you enter the source code instead of the bytecode. Because there is no point entering the bytecode. If you enter the source code you may get the answer more quicker. – Roshana Pitigala Apr 12 '17 at 19:09
  • If you need to know how certain lines perform, there are plenty of ways to profile your code. The most brain-dead simple of these is to literally print or log execution times before and after a given line or method. – Jim Kiley Apr 12 '17 at 20:02
  • Not gonna work Jim.. print and log have their own time burn. It will distort my timing. I just wanna know where are the line numbers! Does javac -g generate line numbers? If yes, then where are they. How does log4j figure out line numbers. – Apurva Singh Apr 12 '17 at 20:05
  • Well, I am not a pro of java bytecode, but I'm pretty sure that you will never see any line number in the bytecode or in any compiled code of any language! This thread seems to have some hints to your second question : http://stackoverflow.com/questions/115008/how-can-we-print-line-numbers-to-the-log-in-java – pwillemet Apr 12 '17 at 21:47

1 Answers1

5

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

Stephan Herrmann
  • 7,963
  • 2
  • 27
  • 38