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
- 1 or more source files to be compiled (such as
MyClass.java).
@argfiles
- 1 or more files that lists options and source files.
DESCRIPTION
- Arrange source files in a directory tree that reflects their
package tree.
E.g., if your
source files are in a directory named 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,
java.applet.Applet
uses
Applet's ancestor classes:
- java.awt.Panel
- java.awt.Container
- java.awt.Component,
- java.awt.Object.
When you extend Applet, you need these classes too.
- When the compiler needs type information, it
looks for a source file or class file which defines the type.
- If the -sourcepath option is specified, the
compiler
searches the indicated path for source files.
- Otherwise, the compiler
searches the user classpath both for class files and source
files.
- A successful type search may produce a class file, a source file,
or
both.
- If the search produces:
- a class file but no source file: javac
uses the class file.
- a source file but no class file: javac
compiles the source file and uses the resulting class file.
- 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
- Again, if the -sourcepath option is unspecified, the
class path is searched for source files and 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.
- Example If you specify -d
/home/myclasses and the class is called
com.mypackage.MyClass
,
then the class file is put in /home/myclasses/com/mypackage/MyClass.class
.
- If -d is unspecified, javac puts the
class
file in the same directory as the source file.
- -help
Print a synopsis of standard options.
- -sourcepath sourcepath
- Specify the source code path to search for class or interface
definitions.
- Source path entries are
separated by a colon (:) 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.
EXAMPLES
Compiling a Simple Program
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 is 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 1 of the source files in the previous
example, we
recompile it:
% 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; the default class path
(the current directory) contains the package directory, greetings.
- Suppose we want to recompile this file and not worry about which
directory we're in?
- Then, we need to use full paths in the class path.
% javac -classpath /student/ito/examples /student/ito/examples/greetings/Hi.java
If we change greetings.Hi
to use a utility,
that utility needs to be accessible via the class path.
% javac -classpath /student/ito/examples:/lib/Banners.jar \
/student/ito/examples/greetings/Hi.java
Separating Source Files and Class Files
- It often makes sense to separate source files from class
files.
- 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.
- In the example below, GoodBye.java is in a package named farewells;
% 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 \
-d classes \
src/farewells/GoodBye.java
% ls classes
farewells/
% ls classes/farewells
Base.class GoodBye.class
- The compiler compiled
Base.java
,
even though we didn't specify it on the command line.
Exercises
- In the exercises below:
- Main.java uses MyQ.java
- Neither has a package statement
- Based on the directory structure below, give a javac command:
- Executed while in the cs50 directory that:
- puts the .class file in the same directory as Main.java
- Executed while in the cs50 directory that compiles:
- compiles MainTest.java
- puts the .class file in the same directory as MainTest.java
- needs classes in junit.jar
- Executed while in the cs50 directory that compiles:
- compiles: MainTest.java
- puts the .class file in the same directory as MainTest.java
- needs classes in junit.jar and jogl.jar
- Executed while in the cs50 directory that compiles:
- compiles: Main.java and MainTest.java
- puts the .class files in the same directory as their respective .java files
- needs classes in junit.jar and jogl.jar
- Executed while in the cs50 directory that compiles:
- compiles: Main.java and MainTest.java
- needs classes in junit.jar and jogl.jar
- puts the .class files in the class directory (what 2 .class files are put in the class tree that are not shown?)
- Executed while in the cs50 directory that compiles, using the -sourcepath option:
- compiles: MainTest.java and MainTest.java
- needs classes in junit.jar and jogl.jar
- puts the .class files in the class directory
- Executed while in the cs50 directory that compiles, using the -sourcepath option:
- compiles: MyQ.java, Main.java, and MainTest.java
- needs classes in junit.jar and jogl.jar
- puts the .class files in the class directory
- Executed while in the cs50 directory that compiles, using the -sourcepath option:
- compiles: MyQ.java, Main.java, and MainTest.java, only if needed.
- needs classes in junit.jar and jogl.jar
- puts the .class files in the class directory
javac -classpath class:library/junit.jar:jogl.jar \
-sourcepath source:test \
-d class \
source/teamname/projectname/Main.java \
test/teamname/projectname/MainTest.java
- Executed while in the smith directory that compiles, using the -sourcepath option:
- compiles: MyQ.java, Main.java, and MainTest.java, only if needed.
- needs classes in junit.jar and jogl.jar
- puts the .class files in the class directory
