Gentle Breeze

[JAVA] javac options 본문

⑨ 직무역량강화/JAVA

[JAVA] javac options

재령 2012. 2. 15. 14:30
- 출처 :  http://letmelove.net/blog/entry/javac-%EC%98%B5%EC%85%98%EA%B3%BC-java%EC%98%B5%EC%85%98 

javac
 

- 자바 컴파일러로서 자바코드를 작성한 소스파일(.java)을 자바 가상머신이 인식할수 있는 바이트 코드(.class)
타입으로 변환시켜주는 명령어 입니다.

- 사용법

javac <options> <souce files>

예를들어, Hello.java, Greeting.java 두개의 파일이 존재한다면,

javac Hello.java Greeting.java

javac *.java (*을 사용해서, 모든 확장자가 .java인 파일을 컴파일할수 있다.)


1) 옵션

a) -classpath

-classpath(cp) path(파일 절대 경로)

컴파일러가 컴파일 하기 위해서 필요로 하는 참조할 클래스 파일들을 찾기 위해서 컴파일시 파일 경로를 지정해주는
옵션. 예를 들어,  Hello.java파일이 C:\Java 디렉터리에 존재하고, 필요한 클래스 파일들이 C:\Java\Engclasses에 위치한다면

javac -classpath C:\Java\Engclasses C:\Java\Hello.java

로 해주면 된다. 만약 참조할 클래스 파일들이 C:\Java\Engclasses 외의 
다른 디렉토리에도 존재 한다면 
C:\Java\Korclasses 일 경우 

javac -classpath C:\Java\Engclasses;C:\Java\Korclasses C:\Java\Hello.java

그리고 현재 디렉토리 역시 포함하고 싶다면

javac -classpath .;C:\Java\Engclasses;C:\Java\Korclasses C:\Java\Hello.java

기본적으로 dos 에서는 .는 현재 디렉토리를 의미하고 ..는 현재 디렉토리의 상위디렉토리를 의미한다. 
또한 classpath 대신 단축어인 cp를 사용해도 된다.

javac -cp C:\Java\Engclasses C:\Java\Hello.java

** 유닉스에서 클래스 경로(classpath) 항목은 콜론(:) 으로 구분한다.
** 윈도우에서 클래스 경로(classpath) 항목은 세미콜론(;) 으로 구분한다.
 

b) -d
 
 
-d directory
 
클래스 파일을 생성할 Root 디렉토리를 지정한다.

기본적으로 컴파일러는 -d 옵션을 주지 않으면 소스파일이 위치한 디렉토리에 클래스 파일을 생성시킨다.
 
예를 들어 Hello.java파일이 C:\Java 디렉토리에 존재하고 클래스 파일의 Root 디렉토리를 C:\Java\Classfiles 라고 하면 

javac -d C:\Java\Classfiles C:\Java\Hello.java

이다.

만약 -d 옵션을 사용하려고 하는데 Root 디렉토리(위예에서는 C:\Java\Classfiles) 가 존재 하지 않는다면 
"The system cannot find the path specified"라는 에러 메시지를 보게 된다. 
 
현재 작업 디렉토리가 C:\Java\Classfiles 에 위치하면
javac -d .\Classfiles Hello.java 와 같이 상대 디렉토리로 표현할수 있다.

java class내에서 package를 선언한 경우 package별 폴더를 생성하고 해당 폴더에(package) compile 한다.

c) -encoding
 
-encoding encoding name
 
소스 파일에 사용된 문자열 인코딩을 설정한다.
만약 위 옵션이 설정되어 있지 않으면 플랫폼의 기본적인 컨버터가 사용되어 진다.

d) -g
 
모든 디버깅 정보를 생성시킨다.
만약 위 옵션이 설정되어 있지 않으면 기본적으로 라인넘버만 생성시킨다.
 
-g:none

디버깅 정보를 전혀 생성 시키지 않는다.

-g:{lines, vars, source}

위처럼 명시적으로, 몇몇 디버깅 정보를 생성시킬수 있다.
lines은 라인정보, vars는 지역변수, sounce는 소스 파일 정보를 나타낸다.

e) -nowarn

경고 메시지 (warning message)를 생성시키지 않는다.

f) -verbose

컴파일러와 링커가 현재 어느 소스파일이 컴파일되고 있고, 어느 파일이 링크되고 있는지 그 정보를 출력한다.

h) -deprecation

소스 코드 내에서 사용되어진 deprecated API의 위치를 출력한다.

eg)
C:\Java> javac World.java
Note: World.java uses a deprecated API. Recompile with "-deprecation" for details.
 
1 warning

C:\Java> javac -deprecation World.java
World.java:52: Note: The method java.awt.Dimension size() in class java.awt.Compon
ent has been deprecated.

Dimension d = size();

Note: World.java uses a deprecated API. Please consult the documentation for a be
tter alternative.

i) -sourcepath

-sourcepath 소스패스

소스파일의 위치를 지정한다.

j) -target

-target 자바 가상머신버전

지정된 자바 가상머신 버전의 VM에서 작동되어지도록 클래스 파일을 생성시킨다.

1.1
jvm 1.1 버전에서 호환되어 질 수 있는 클래스 파일생성
 
1.2
jvm 1.2 버전에서 호환되어 질 수 있는 클래스 파일생성

1.3
jvm 1.3 버전에서 호환되어 질 수 있는 클래스 파일 생성

ex)
javac -target 1.2 Helloworld.java

k) -bootclasspath 패스

특정한 bootstrap 또는 확장 클래스를 지정할 수 있다.
 
기본적으로 자바 컴파일러는 javac 가 설치된 플랫폼의 bootstrap과 확장 클래스들을 통해서 컴파일 작업을 수행하지만 bootclasspath 옵션을 사용하면 cross-compiling 이라고 해서 다른 JAVA 플랫폼의 bootstrap과 확장 클래스들을 통해서 compile 할 수 있는 기능을 지원한다.
 
예를들어

javac -target 1.1 -bootclasspath jdk1.1.7/lib/classes.zip -extdirs "" OldCode.java

컴파일러에게 현재 자신의 bootstrap을 사용하지 말고 jdk1.1.7/lib/classes.zip bootstrap 클래스들을 사용해서 컴파일 하라고 
명령하는것이다.
 
참고로 mobile JAVA에서 모바일폰에 설정 된 jvm에 맞도록 소스코드를 컴파일하기 위해서 주로 사용되어지는 옵션이다.

l) -extdirs 디렉토리

특정한 확장 디렉토리를 지정한다. cross-compiling 시 주로 사용되어지는 옵션이면 각 디렉토리들은 콜론(:)에 의해서 분리되어진다. 
컴파일 시점에 기술한 디렉토리의 클래스 파일을 참조한다.


M) package 선언된 Java class 실행

java package.class명 (현 위치 아래에 package폴더가 존재)
 
package 위치는 CLASSPATH 하위 폴더부터로 인식한다. 따라서 Package 폴더의 Root가 ClassPath에 지정되든지 .(현 위치)를 ClassPath에 등록 후 현 위치 아래서부터 package의 위치를 지정한다.

java com.test.SampleMain

###################################################################
javac options
###################################################################

Usage: javac <options> <source files>
where possible options include:
  -g                                  Generate all debugging info
  -g:none                          Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                         Generate no warnings
  -verbose                        Output messages about what the compiler is doing
  -deprecation                   Output source locations where deprecated APIs are used
  -classpath <path>           Specify where to find user class files and annotation processors
  -cp <path>                     Specify where to find user class files and annotation processors
  -sourcepath <path>         Specify where to find input source files
  -bootclasspath <path>      Override location of bootstrap class files
  -extdirs <dirs>                Override location of installed extensions
  -endorseddirs <dirs>       Override location of endorsed standards path
  -proc:{none,only}            Control whether annotation processing and/or compilation is done.
  -processor <class1>[,<class2>,<class3>...]

                                      Names of the annotation processors to run; bypasses default discovery process
  -processorpath <path>      Specify where to find annotation processors
  -d <directory>                 Specify where to place generated class files
  -s <directory>                 Specify where to place generated source files
  -implicit:{none,class}      Specify whether or not to generate class files for implicitly referenced files
  -encoding <encoding>       Specify character encoding used by source files
  -source <release>          Provide source compatibility with specified release
  -target <release>            Generate class files for specific VM version
  -version                         Version information
  -help                             Print a synopsis of standard options
  -Akey[=value]                Options to pass to annotation processors
  -X                                 Print a synopsis of nonstandard options
  -J<flag>                         Pass <flag> directly to the runtime system
  
  
  
  
Usage: java [-options] class [args...]
          (to execute a class)
  or  java [-options] -jar jarfile [args...]
          (to execute a jar file)

where options include:
   -client   to select the "client" VM
   -server   to select the "server" VM
   -hotspot   is a synonym for the "client" VM  [deprecated]
                 The default VM is client.
                 
   -cp <class search path of directories and zip/jar files>
   -classpath <class search path of directories and zip/jar files>
                 A ; separated list of directories, JAR archives, and ZIP archives to search for class files.
   -D<name>=<value>
                 set a system property
   -verbose[:class|gc|jni]
                 enable verbose output
   -version      print product version and exit
   -version:<value>
                 require the specified version to run
   -showversion  print product version and continue
   -jre-restrict-search | -jre-no-restrict-search
                 include/exclude user private JREs in the version search
   -? -help      print this help message
   -X            print help on non-standard options
   -ea[:<packagename>...|:<classname>]
   -enableassertions[:<packagename>...|:<classname>]
                 enable assertions
   -da[:<packagename>...|:<classname>]
   -disableassertions[:<packagename>...|:<classname>]
                 disable assertions
   -esa | -enablesystemassertions
                 enable system assertions
   -dsa | -disablesystemassertions
                 disable system assertions
   -agentlib:<libname>[=<options>]
                 load native agent library <libname>, e.g. -agentlib:hprof
                   see also, -agentlib:jdwp=help and -agentlib:hprof=help
   -agentpath:<pathname>[=<options>]
                 load native agent library by full pathname
   -javaagent:<jarpath>[=<options>]
                 load Java programming language agent, see java.lang.instrument
   -splash:<imagepath>
                 show splash screen with specified image
 
Comments