Pablo Matias Gomez
Pablo Matias Gomez

Reputation: 6823

How is the stacktrace printed when the program is compiled?

This is a very simple question:

When you compile a java program, it is converted to byte code, so therefore, every line number of the .java or .class file is missed (I think so, probably I am wrong..). So, when you print a stack trace, how does it manage to get all the class names and line numbers that were in the call stack? I think that I may be missing something here, but I couldn't find anything related to this.

Upvotes: 1

Views: 465

Answers (2)

Keith
Keith

Reputation: 3151

If line numbers are present, then the java compiler created bytecode with the debug flag set to true. This can be achieved using java -g

From Oracle's javac documentation:

  • -g
    • Generate all debugging information, including local variables. By default, only line number and source file information is generated.
  • -g:none
    • Do not generate any debugging information.
  • -g:{keyword list} - Generate only some kinds of debugging information, specified by a comma separated list of keywords. Valid keywords are:
    • source
      • Source file debugging information
    • lines
      • Line number debugging information
    • vars
      • Local variable debugging information

Upvotes: 5

user207421
user207421

Reputation: 310978

When you compile a java program, it is converted to byte code

Correct.

so therefore, every line number of the .java or .class file is missed (I think so, probably I am wrong..).

You're wrong. Line number information is embedded into the .class file unless you use the -g compiler option in certain ways.

Upvotes: 7

Related Questions