Search code examples
javacompiler-constructionstack-tracebytecode

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.


Solution

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