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