반응형

자바(JAVA) 실습 - javap 명령어 정리

 

 

<< 환경 >>

OpenJDK 1.8 (openjdk version 1.8.0_292)

 

 

<< javap >>

javap 명령어는 하나 이상의 class를 역어셈블한다.

javap의 결과값은 사용한 옵션에 따라 달라진다.

javap 옵션 없이 실행하는 경우 class의 package, protected, public 변수와 메소드를 표시한다.

javap는 결과값을 stdout으로 출력한다.

 

 

<< javap command line option 확인 >>

javap  <options>  <classes>

 

where possible options include:

  -help  --help  -?   Print this usage message
  -version   Version information
  -v  -verbose   Print additional information
  -l   Print line number and local variable tables
  -public   Show only public classes and members
  -protected   Show protected/public classes and members
  -package   Show package/protected/public classes and members (default)
  -p  -private   Show all classes and members
  -c   Disassemble the code
  -s   Print internal type signatures
  -sysinfo   Show system info (path, size, date, MD5 hash) of class being processed
  -constants   Show final constants
  -classpath <path>   Specify where to find user class files
  -cp <path>   Specify where to find user class files
  -bootclasspath <path>   Override location of bootstrap class files

 

 

 

[ TestJavap.java ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package javap.test;
 
public class TestJavap {
    
    public static final int NUM_ZERO = 0;
    protected static final int NUM_ONE = 1;
    private static final int NUM_TWO = 2;
    
    public static void main(String[] args) {
        String str = "verbose";
        protectedPrintTest(str);
        privatePrintTest(str);
    }
    
    protected static void protectedPrintTest(String str) {
        String test = "protected";
        System.out.println("javap " + str + " " + test);
    }
    
    private static void privatePrintTest(String str) {
        String test = "private";
        System.out.println("javap " + str + " " + test);
    }
 
}
 

 

 

-version

          자바 버전 출력

          This option prints Version information of java.

      사용법 (Syntax):

          javap -version

 

1
2
user@Dui-MacBookPro ~ % javap -version
1.8.0_292

 

 

-v or -verbose 

          추가정보 출력 - 스택사이즈, 메소드의 변수의 개수 및 변수값

          Prints stack size, number of locals and arguments for methods.

      사용법 (Syntax):

          javap -v {class_name}

 

javap -v TestJavap.class

 

main

 

protectedPrintTest method

 

 

-l

          줄과 지역변수(로컬변수) 테이블 출력

          Prints line and local variable tables.

      사용법 (Syntax):

          javap -l {class_name}

 

javap -l TestJavap.class

 

 

-public

          pulic class와 member만 표시

          Shows only public classes and members.

      사용법 (Syntax):

          javap -public {class_name}

 

javap -public TestJavap.class

 

 

-protected

          protected / pulic class와 member 표시

          Shows only protected and public classes and members.

      사용법 (Syntax):

          javap -protected {class_name}

 

javap -protected TestJavap.class

 

 

-package

          package / protected / pulic class와 member 표시 (기본 표시 방법)

          Shows only package, protected, and public classes and members. This is the default.

      사용법 (Syntax):

          javap -package {class_name}

 

javap -package TestJavapclass

 

 

-p or -private

          모든 class와 member 표시

          Shows all classes and members.

      사용법 (Syntax):

          javap -private {class_name}

 

javap -private TestJavap.class

 

 

-c

          클래스의 각 메소드의 역어셈블된(바이너리코드를 바이트코드로 변환) 코드 표시 (ex. Java 바이트코드를 구성하는 명령어)

          Prints disassembled code, for example, the instructions that comprise the Java bytecodes, for each of the methods in the class.

      사용법 (Syntax):

          javap -c {class_name}

 

javap -c TestJavap.class

 

 

-s

          내부 타입을 표시

          Prints internal type signatures.

      사용법 (Syntax):

          javap -s {class_name}

 

javap -s TestJavap.class

 

 

-sysinfo

          해당 클래스의 시스템 정보 표시 (경로, 용량, 날짜, MD5 hash)

          Shows system information (path, size, date, MD5 hash) of the class being processed.

          * MD5 hash: MD5 message-digest algorithm의 hash 함수를 사용하여 생성한 혹은 인코딩한 128bit의 hash값

                              동일한 문자열을 MD5 알고리즘으로 인코딩할 경우 항상 동일한 128bit의 hash값을 얻는다.

      사용법 (Syntax):

          javap -sysinfo {class_name}

 

javap -sysinfo TestVerbose.class

 

 

-constants

          상수 표시

          Shows static final constants.

      사용법 (Syntax):

          javap -constants {class_name}

 

javap -constants TestJavap.class

 

 

-classpath or -cp <path>

          class들을 관찰하기 위해 javap 명령어를 사용하기 위한 경로를 설정. 설정시 기본 CLASSPATH 환경변수를 대체.

          Specifies the path the javap command uses to look up classes. Overrides the default or the CLASSPATH environment variable when it is set.

      사용법 (Syntax):

          javap -classpath {path}

 

javap -classpath /usr/libexec/java_home TestJavap.class

 

 

-bootclasspath <path>

          bootstrap class들을 기동할 경로를 지정. 기본적으로 bootstrap class들은 jre/lib/jr.jar 와 기타 JAR파일에 위차한 주요 Java 플랫폼을 구현한다.

          Specifies the path from which to load bootstrap classes. By default, the bootstrap classes are the classes that implement the core Java platform located in jre/lib/rt.jar and several other JAR files.

      사용법 (Syntax):

          javap -bootclasspath {path}

 

cd /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib

find . -name "*.jar"

 

javap -bootclasspath /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib TestJavap.class

 


참고

https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javap.html

 

반응형