1

我正在尝试理解 Java 字节码。我使用命令分析我的简单 HelloWorld javap -c -v -l -s HelloWorld.class。但是,我看不到 LocalVariableTable 条目。这是为什么?它被忽略了吗?我在 OS X 10.10.4 上运行 java 1.8.0_45。感谢您的帮助,谢谢!

  1 public class HelloWorld {
  2 
  3         public static void main(String[] args) {
  4                 String str = "Hello ";
  5                 System.out.println(str + args[0]);
  6         }
  7 }

结果:

  Last modified Aug 2, 2015; size 590 bytes
  MD5 checksum 59cf5ec143d5c28898d6b9bec3140379
  Compiled from "HelloWorld.java"
public class HelloWorld
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #10.#19        // java/lang/Object."<init>":()V
   #2 = String             #20            // Hello
   #3 = Fieldref           #21.#22        // java/lang/System.out:Ljava/io/PrintStream;
   #4 = Class              #23            // java/lang/StringBuilder
   #5 = Methodref          #4.#19         // java/lang/StringBuilder."<init>":()V
   #6 = Methodref          #4.#24         // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   #7 = Methodref          #4.#25         // java/lang/StringBuilder.toString:()Ljava/lang/String;
   #8 = Methodref          #26.#27        // java/io/PrintStream.println:(Ljava/lang/String;)V
   #9 = Class              #28            // HelloWorld
  #10 = Class              #29            // java/lang/Object
  #11 = Utf8               <init>
  #12 = Utf8               ()V
  #13 = Utf8               Code
  #14 = Utf8               LineNumberTable
  #15 = Utf8               main
  #16 = Utf8               ([Ljava/lang/String;)V
  #17 = Utf8               SourceFile
  #18 = Utf8               HelloWorld.java
  #19 = NameAndType        #11:#12        // "<init>":()V
  #20 = Utf8               Hello
  #21 = Class              #30            // java/lang/System
  #22 = NameAndType        #31:#32        // out:Ljava/io/PrintStream;
  #23 = Utf8               java/lang/StringBuilder
  #24 = NameAndType        #33:#34        // append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #25 = NameAndType        #35:#36        // toString:()Ljava/lang/String;
  #26 = Class              #37            // java/io/PrintStream
  #27 = NameAndType        #38:#39        // println:(Ljava/lang/String;)V
  #28 = Utf8               HelloWorld
  #29 = Utf8               java/lang/Object
  #30 = Utf8               java/lang/System
  #31 = Utf8               out
  #32 = Utf8               Ljava/io/PrintStream;
  #33 = Utf8               append
  #34 = Utf8               (Ljava/lang/String;)Ljava/lang/StringBuilder;
  #35 = Utf8               toString
  #36 = Utf8               ()Ljava/lang/String;
  #37 = Utf8               java/io/PrintStream
  #38 = Utf8               println
  #39 = Utf8               (Ljava/lang/String;)V
{
  public HelloWorld();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=4, locals=2, args_size=1
         0: ldc           #2                  // String Hello
         2: astore_1
         3: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
         6: new           #4                  // class java/lang/StringBuilder
         9: dup
        10: invokespecial #5                  // Method java/lang/StringBuilder."<init>":()V
        13: aload_1
        14: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        17: aload_0
        18: iconst_0
        19: aaload
        20: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
        23: invokevirtual #7                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        26: invokevirtual #8                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        29: return
      LineNumberTable:
        line 4: 0
        line 5: 3
        line 6: 29
}
SourceFile: "HelloWorld.java"
4

1 回答 1

2

您打算使用javap's -loption

打印出行和局部变量表。

为了使它们可用,您需要使用javac'-g选项编译源文件

生成所有调试信息,包括局部变量。默认情况下,只生成行号和源文件信息。

于 2015-08-03T01:05:03.363 回答