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

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.

SEARCHING FOR TYPES

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.

OPTIONS

Standard Options

-classpath classpath
-d directory
-help
Print a synopsis of standard options.
-source 1.4
Enables compiling source code containing assertions.
-sourcepath sourcepath

COMMAND LINE ARGUMENT FILES

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

% 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
% 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

% 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