본문 바로가기
Java/topic

java, javac, javap, jar 명령어 간략 정리

by Ellery 2022. 4. 7.
  • 스프링 프레임워크에서 지원하는 최소한의 자바버전은 8버전이다.
    일부 메이븐 플러그인이 컴파일 버전을 고려하지 않고 만들었다. 9이상 버전을 사용한 플러그인이 있음. 그래서 문제가 생길 수 있다. → java.lang.UnsupportedClassVersionError가 발생함. 

  • 이 경우에 8 버전으로 컴파일을 해야한다. cross-compiliation 옵션들을 이용할 수 있다
# 1.8 이전 버전으로 크로스 컴파일하는 경우
javac -bootclasspath "C:\Apps\Java\jdk1.8.0_31\jre\lib\rt.jar" \
  -source 1.8 -target 1.8 com/Ellery/MajorMinorApp.java

# 1.9 이후부터
javac --release 8 com/Ellery/MajorMinorApp.java
  • javac [options....] [sourcefiles] : 옵션은 여러개 이어서 사용 가능.
    ex) javac -cp 참조파일경로 -encoding 문자열설정 -d 루트디렉토리 source.java
    • -version: Version information
    • -v, -verbose: Print additional information
    • -I: 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, cp <path>: 컴파일 시에 필요로 하는 참조할 클래스파일의 파일경로를 지정
    • -d <root directory>: 클래스 파일을 지정할 루트 디렉토리 설정
    • -encoding <encoding set>: 소스 파일에 사용될 문자열 설정
    • -g(:none)(:line)(:var)(:source): 디버깅 정보 옵션. 라인정보, 지역변수, 소스 파일 정보
    • -nowarn: 경고메세지 생성하지 않음
    • -sourcepath: 소스파일의 위치를 지정
    • -source <version>: The version that your source code requires to compile.
    • -target <version>: 지정된 버전의 JVM에서 작동되도록 클래스파일을 생성한다
    • -classpath, cp <path>: 컴파일 시에 필요로 하는 참조할 클래스파일의 파일경로를 지정
    • -bootclasspath <path>: override location of bootstrap class files
      • Cross-compile against the specified set of boot classes. As with the user class path, boot class path entries are separated by colons (:) and can be directories, JAR archives, or ZIP archives.
    • -extdirs <directories>: 특정한 부트스트랩 혹은 익스텐션 클래스를 지정한다. (크로스 컴파일링용)

 

 

  • java -classpath <class 파일 위치> <클래스 이름>  - 클래스 이름 앞에 반드시 패키지 이름까지 붙여야 함
  • javap -verbos 클래스명 | find /N "version"
  • jar <options> <jar 파일 이름> <최상위 패키지 경로> - .jar 파일을 만드는 명령어 
    • options: ex) cfm (c:create, f: 만들어질 .jar 파일의 이름을 지정. f 뒤에 파일명이 와야 함, m: manifest 파일도 함께 넣겠다)
      - 다른 option들은 https://docs.oracle.com/en/java/javase/11/tools/jar.html
    • ex) jar cfm ..\lib\hello.jar ..\src\manifest.txt test
    • jar 파일 내부에 META-INF\MANIFEST.MF 라는 파일이 있음
      • 자바 애플리케이션의 정보를 담고 있는 메타데이터 파일. .jar 파일을 만들 때 이 파일을 같이 넣어줄 수 있음
      • .jar 파일의 시작점(메인 함수)에 대한 정보를 넣어야 함

 

 

참고:

https://docs.oracle.com/en/java/javase/11/tools/javac.html#GUID-AEEC9F07-CB49-4E96-8BC7-BCC2C7F725C9

 

Tools Reference

You can use the javac tool and its options to read Java class and interface definitions and compile them into bytecode and class files.

docs.oracle.com

https://www.baeldung.com/java-lang-unsupportedclassversion