有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java编译程序时,stacktrace是如何打印的?

这是一个非常简单的问题:

当你们编译一个java程序时,它会被转换成字节码,所以,每行代码的每一个行号。java或。类文件丢失(我想是的,可能是我错了)。那么,当打印堆栈跟踪时,它是如何获得调用堆栈中的所有类名和行号的呢?我想我可能遗漏了一些东西,但我找不到任何与此相关的东西


共 (2) 个答案

  1. # 1 楼答案

    如果存在行号,那么java编译器创建字节码时debug标志设置为true。这可以通过java -g实现

    来自Oracle的javac文档:

    • -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
  2. # 2 楼答案

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

    你错了。行号信息嵌入到。类文件,除非以某些方式使用-g编译器选项