zoukankan      html  css  js  c++  java
  • Java文件编译与反编译:javac命令和javap命令

    1.创建一个Test.java文件,并输入内容

    public class Test{
        private int m;
        public int inc(){
            return m + 1;
        }
    }

    2.使用javac命令编译Test.java文件,得到Test.class文件

    javac Test.java 

    3.使用Sublime3打开Test.class

    cafe babe 0000 0034 0013 0a00 0400 0f09
    0003 0010 0700 1107 0012 0100 016d 0100
    0149 0100 063c 696e 6974 3e01 0003 2829
    5601 0004 436f 6465 0100 0f4c 696e 654e
    756d 6265 7254 6162 6c65 0100 0369 6e63
    0100 0328 2949 0100 0a53 6f75 7263 6546
    696c 6501 0009 5465 7374 2e6a 6176 610c
    0007 0008 0c00 0500 0601 0004 5465 7374
    0100 106a 6176 612f 6c61 6e67 2f4f 626a
    6563 7400 2100 0300 0400 0000 0100 0200
    0500 0600 0000 0200 0100 0700 0800 0100
    0900 0000 1d00 0100 0100 0000 052a b700
    01b1 0000 0001 000a 0000 0006 0001 0000
    0001 0001 000b 000c 0001 0009 0000 001f
    0002 0001 0000 0007 2ab4 0002 0460 ac00
    0000 0100 0a00 0000 0600 0100 0000 0400
    0100 0d00 0000 0200 0e

    4.使用javap命令,反编译Test.class文件

    javap -v Test

    输出为:

    Classfile /Users/soldier/Desktop/Test.class
      Last modified 2018-9-19; size 265 bytes
      MD5 checksum 551399dd9890a81e8f8c079c6c1f364d
      Compiled from "Test.java"
    public class Test
      minor version: 0
      major version: 52
      flags: ACC_PUBLIC, ACC_SUPER
    Constant pool:
       #1 = Methodref          #4.#15         // java/lang/Object."<init>":()V
       #2 = Fieldref           #3.#16         // Test.m:I
       #3 = Class              #17            // Test
       #4 = Class              #18            // java/lang/Object
       #5 = Utf8               m
       #6 = Utf8               I
       #7 = Utf8               <init>
       #8 = Utf8               ()V
       #9 = Utf8               Code
      #10 = Utf8               LineNumberTable
      #11 = Utf8               inc
      #12 = Utf8               ()I
      #13 = Utf8               SourceFile
      #14 = Utf8               Test.java
      #15 = NameAndType        #7:#8          // "<init>":()V
      #16 = NameAndType        #5:#6          // m:I
      #17 = Utf8               Test
      #18 = Utf8               java/lang/Object
    {
      public Test();
        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 int inc();
        descriptor: ()I
        flags: ACC_PUBLIC
        Code:
          stack=2, locals=1, args_size=1
             0: aload_0
             1: getfield      #2                  // Field m:I
             4: iconst_1
             5: iadd
             6: ireturn
          LineNumberTable:
            line 4: 0
    }
    SourceFile: "Test.java"

    使用命令:

    javap -c Test

    输出为:

    Compiled from "Test.java"
    public class Test {
      public Test();
        Code:
           0: aload_0
           1: invokespecial #1                  // Method java/lang/Object."<init>":()V
           4: return
    
      public int inc();
        Code:
           0: aload_0
           1: getfield      #2                  // Field m:I
           4: iconst_1
           5: iadd
           6: ireturn
    }

    用法摘要:

    -help 帮助
    -l 输出行和变量的表
    -public 只输出public方法和域
    -protected 只输出public和protected类和成员
    -package 只输出包,public和protected类和成员,这是默认的
    -p -private 输出所有类和成员
    -s 输出内部类型签名
    -c 输出分解后的代码,例如,类中每一个方法内,包含java字节码的指令,
    -v 输出栈大小,方法参数的个数
    -constants 输出静态final常量

  • 相关阅读:
    Apache Struts 2.3.12 GA?
    emacs配置《转》
    vim配置
    vim插件
    git使用
    ubuntu常用设置
    Eclipse如何关联已经clone的Git项目
    变量名、对象引用(指针)与堆栈
    Web项目转换为groovy项目的步骤
    日志 20071208(SvcUtil.exe,高并发网站架构)
  • 原文地址:https://www.cnblogs.com/sunweiye/p/11008427.html
Copyright © 2011-2022 走看看