JVM class反编译

nix.long 于 2017-10-20

文章目录

    • 1. Code Demo
    • 2.javac
    • 3.javap
    • 4.Bat脚本

1. Code Demo

[java] 示例demo, 保存为Main.java

package com.test.Main;
public class Main{
    void main(String args[]){
        int i = 0;
        i = 5;
        return;
    }
}

2.javac

javac Main.java 生成 Main.class

javac Main.java

3.javap

使用javap命令反编译class文件,查看助记符指令

javap -c -v Main.class

Classfile /G:/Mess/test/Main.class
  Last modified 2017-10-16; size 279 bytes
  MD5 checksum a8b7efbdef220ee87f1d79cd549cf26c
  Compiled from "Main.java"
public class com.test.Main.Main
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #3.#12         // java/lang/Object."":()V
   #2 = Class              #13            // com/test/Main/Main
   #3 = Class              #14            // java/lang/Object
   #4 = Utf8               <init>
   #5 = Utf8               ()V
   #6 = Utf8               Code
   #7 = Utf8               LineNumberTable
   #8 = Utf8               main
   #9 = Utf8               ([Ljava/lang/String;)V
  #10 = Utf8               SourceFile
  #11 = Utf8               Main.java
  #12 = NameAndType        #4:#5          // "":()V
  #13 = Utf8               com/test/Main/Main
  #14 = Utf8               java/lang/Object
{
  public com.test.Main.Main();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."":()V
         4: return
      LineNumberTable:
        line 3: 0
  void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags:
    Code:
      stack=1, locals=3, args_size=2
         0: iconst_0
         1: istore_2
         2: iconst_5
         3: istore_2
         4: return
      LineNumberTable:
        line 6: 0
        line 7: 2
        line 8: 4
}
SourceFile: "Main.java"

其中, javap用法:

javap

用法: javap  
其中, 可能的选项包括:
  -help  --help  -?        输出此用法消息
  -version                 版本信息
  -v  -verbose             输出附加信息
  -l                       输出行号和本地变量表
  -public                  仅显示公共类和成员
  -protected               显示受保护的/公共类和成员
  -package                 显示程序包/受保护的/公共类
                           和成员 (默认)
  -p  -private             显示所有类和成员
  -c                       对代码进行反汇编
  -s                       输出内部类型签名
  -sysinfo                 显示正在处理的类的
                           系统信息 (路径, 大小, 日期, MD5 散列)
  -constants               显示最终常量
  -classpath         指定查找用户类文件的位置
  -cp                指定查找用户类文件的位置
  -bootclasspath     覆盖引导类文件的位置

4.Bat脚本

java2opcode.bat

REM =====================================================================
REM Author:     nix.long
REM Date:        2017.10.17
REM Function:     compile x.java to jvm byte-code    
REM =====================================================================

@echo off
if "%1"=="" (
    goto help
)
set input=%1%
IF %input% == help (
    goto help
) ELSE IF %input% == clean (
    IF EXIST *.class (
        del /f /s /q *.class
    )
    
    IF EXIST *.dex (
        del /f /s /q *.dex
    )
) ELSE (
    IF EXIST %input%.class (
        del /f /s /q %input%.class
    )
    
    IF EXIST %input%.dex (
        del /f /s /q %input%.dex
    )
    echo 'to make:'  %input%.java
    echo 'to gen class:'  %input%.class
    javac %input%.java
    echo 'to show opcode:'
    echo -----------------------------------------------------------------
    echo=
    echo=
    echo=
    
    javap -v -p -s %input%.class
    
    echo=
    echo=
    echo=
    echo -----------------------------------------------------------------
)
goto end
:help
    echo -----------------------------------------------------------------
    echo     help [JVM Bytecode]:
    echo        java2opcode.bat help     - show help
    echo        java2opcode.bat clean    - clean mid-files
    echo        java2opcode.bat X        - gen bytecode for X.java
    echo -----------------------------------------------------------------
:end

你可能感兴趣的:(JVM,Java,class,java)