javac - Java programming language compiler
This page is an abbreviated version of
http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/javac.html
SYNOPSIS
javac [ options ] [ sourcefiles ] [ @argfiles ]
Arguments may be in any order.
options
- Command-line options.
sourcefiles
- One or more source files to be compiled (such as
MyClass.java).
@argfiles
- One or more files that lists options and source files.
DESCRIPTION
- In the description that follows, assume that the javac command is
executed when your working directory is projects.
- Arrange source files in a directory tree that reflects their
package tree.
For example, if you keep all your
source files in source,
the source code for com.mysoft.mypack.MyClass
should be
in source/com/mysoft/mypack/MyClass.java.
- By default, the compiler puts each class file in the same
directory as
its source file.
SEARCHING FOR TYPES
- The compiler needs type
information for every class or interface used, extended, or implemented
in the source file.
- This includes classes and interfaces not explicitly
mentioned in the source file but which provide information through
inheritance.
For example, when you subclass java.applet.Applet,
you are
also
using Applet's ancestor classes: java.awt.Panel, java.awt.Container,
java.awt.Component, and java.awt.Object.
- When the compiler needs type information, it
looks for a source file or class file which defines the type.
- The compiler search order is:
- bootstrap classes
- extension classes
- user class path.
- The user class path is defined by using the -classpath
command line option.
- If you use the -sourcepath option, the
compiler
searches the indicated path for source files.
- Otherwise, the compiler
searches the user class path both for class files and source
files.
- A successful type search may produce a class file, a source file,
or
both.
- Here is how javac handles each situation:
- Search produces a class file but no source file: javac
uses the class file.
- Search produces a source file but no class file: javac
compiles the source file and uses the resulting class file.
- Search produces both a source file and a class file:
- If the class file is
out of date, javac recompiles the source file and uses the
updated class file.
- Otherwise, javac uses the class file.
- javac considers a class file out of date only if it
is older than the source file.
- javac can silently compile source
files
not mentioned
on the command line.
- Use the -verbose option to trace automatic
compilation.
OPTIONS
Standard Options
- -classpath classpath
- Set the user class path.
- Again, if the -sourcepath option is not specified, the
user
class path is searched for source files as well as class files.
- -d directory
- Set the destination directory for class files.
- The
destination
directory must already exist; javac does not create the destination
directory.
- If a class is part of a package, javac puts the
class file in a subdirectory reflecting the package name, creating
directories as needed.
- For example, if you specify -d
/home/myclasses and the class is called
com.mypackage.MyClass
,
then the class file is called /home/myclasses/com/mypackage/MyClass.class
.
- If -d is not specified, javac puts the
class
file in the same directory as the source file.
- -help
Print a synopsis of standard options.
- -source 1.4
Enables compiling source code
containing
assertions.
- -sourcepath sourcepath
- Specify the source code path to search for class or interface
definitions.
- Source path entries are
separated by colons (:) and can be directories, JAR archives, or
ZIP archives.
- If packages are used, the local path name within the
directory or archive must reflect the package name.
COMMAND LINE ARGUMENT FILES
- To shorten or simplify the javac command line, you can
specify one or more files that themselves contain
arguments to the
javac
command.
- This enables you to
create javac commands of any length on
any operating
system.
- An argument file can include javac options and source filenames
in
any combination.
- The arguments within a file can be space-separated or
newline-separated.
- Filenames within an argument file are relative to
the current directory, not the location of the argument file.
- Wildcards (*) are not allowed in these lists (such as for
specifying
*.java
).
- Use of the '@' character to
recursively interpret files is not supported.
- When executing javac,
pass in the path and name of each argument
file with the '@' leading character.
- When javac encounters an argument beginning with the character `@',
it expands the contents of
that file into the argument list.
Example - Single Arg File
You could use a single argument file
named "argfile
" to
hold all javac arguments:
C:> javac @argfile
This argument file could contain the
contents of both files shown
in the next example.
Example - Two Arg Files
You can create two argument files --
one for the javac options and the
other for the source filenames:
(Notice the following lists have no line-continuation characters.)
Create a file named "options
"
containing:
-d classes
-sourcepath \java\pubs\ws\1_4\src\share\classes
Create a file named "classes
"
containing:
MyClass1.java
MyClass2.java
MyClass3.java
You would then run javac with:
C:> javac @options @classes
Example - Arg Files with Paths
The argument files can have paths, but
any filenames inside
the files are relative to the current working directory (not path1
or path2
):
C:> javac @path1\options @path2\classes
EXAMPLES
Compiling a Simple Program
- One source file,
Hello.java
, defines a
class
called greetings.Hello.
- The
greetings
directory
is the
package directory both for the source file and the class file and is
in the current directory.
- This allows us to use the default user class
path.
- It also makes it unnecessary to specify a separate destination
directory with -d.
% ls
greetings/
% ls greetings
Hello.java
% cat greetings/Hello.java
package greetings;
public class Hello {
public static void main(String[] args) {
for (int i=0; i < args.length; i++) {
System.out.println("Hello " + args[i]);
}
}
}
% javac greetings/Hello.java
% ls greetings
Hello.class Hello.java
% java greetings.Hello World Universe Everyone
Hello World
Hello Universe
Hello Everyone
Compiling Multiple Source Files
This example compiles all the source files in the package greetings
.
% ls
greetings/
% ls greetings
Aloha.java GutenTag.java Hello.java Hi.java
% javac greetings/*.java
% ls greetings
Aloha.class GutenTag.class Hello.class Hi.class
Aloha.java GutenTag.java Hello.java Hi.java
Specifying a User Class Path
Having changed one of the source files in the previous
example, we
recompile it:
% pwd
/examples
% javac greetings/Hi.java
- Since
greetings.Hi
refers to other classes in the greetings
package, the compiler needs to find these
other
classes.
- The example above works, because our default user class path
happens to be the directory containing the package directory.
- But
suppose we want to recompile this file and not worry about which
directory we're in?
- Then, we need to add
/examples
to the
user class path.
% javac -classpath /examples /examples/greetings/Hi.java
If we change greetings.Hi
again, to use a banner utility,
that utility also needs to be accessible through the user class path.
% javac -classpath /examples:/lib/Banners.jar /examples/greetings/Hi.java
To execute a class in greetings
, we need access to greetings
and to the classes it uses.
% java -classpath /examples:/lib/Banners.jar greetings.Hi
Separating Source Files and Class Files
- It often makes sense to keep source files and class
files
in separate
directories on large projects.
- We use -d to
indicate
the separate class file destination.
- Since the source files are not in
the user class path, we use -sourcepath to direct the compiler
to them.
% ls
classes/ lib/ src/
% ls src
farewells/
% ls src/farewells
Base.java GoodBye.java
% ls lib
Banners.jar
% ls classes
% javac -sourcepath src -classpath classes:lib/Banners.jar src/farewells/GoodBye.java -d classes
% ls classes
farewells/
% ls classes/farewells
Base.class GoodBye.class
- The compiler compiled
src/farewells/Base.java
,
even though we didn't specify it on the command line.
- To trace
automatic compiles, use the -verbose option.
