Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.00

'javac' - Java Compilation Command and Options

This section describes what are the commonly used command line options for the Java compiler - 'javac' tool.

"javac": A command line tool that reads Java source files and compiles them into bytecode class files. "javac" is distributed as part of the Sun JDK package and represented by the \j2sdk1.5.0\bin\javac.exe program file. It has the following syntax:

javac [options] [sourcefiles]

where "options" is a list of options and "sourcefiles" is a list of Java source files.

Commonly used options are:

  • "-help" - Displays a short help text.
  • "-verbose" - Generates verbose output to standard output.
  • "-classpath classpath" - Specifies a list of path names where the compiler will search for compiled type definitions. If "-classpath" is not specified, the current directory will be used as the class path.
  • "-sourcepath sourcepath" - Specifies a list of path names where the compiler will search for source type definitions. If "-source" is not specified, the current directory will be used as the source path.
  • "-d directory" - Specifies the directory where the compiler will store the generated class files. If "-d" is not specified, the class files will be stored in the same places as the source files.
  • "-g | -g:none" - Asks the compiler to generate debugging information into the class files or generates no debugging information at all.

If you run the "javac" command without any options, you will get the quick usage information as shown below:

C:\>\j2sdk1.5.0\bin\javac

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
 -cp <path>             Specify where to find user class files
 -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
 -d <directory>         Specify where to place generated class 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
 -X                     Print a synopsis of nonstandard options
 -J<flag>               Pass <flag> directly to the runtime system

Sections in This Chapter

'javac' - Java Compilation Command and Options

Compiling Hello.java - My First Java Program

Option '-classpath' - Specifying Class Path

Option '-sourcepath' - Specifying Source Path

Option '-d' - Specifying Output Directory

Two Types of 'import' Statements

'import' Statements Processed by 'javac'

Option "-g" - Controlling Debugging Information

Dr. Herong Yang, updated in 2008
'javac' - Java Compilation Command and Options