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

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.

SEARCHING FOR TYPES

For example, java.applet.Applet uses Applet's ancestor classes:
When you extend Applet, you need these classes too.

OPTIONS

Standard Options

-classpath classpath
-d directory
-help
Print a synopsis of standard options.
-sourcepath sourcepath

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 1 of the source files in the previous example, we recompile it:
% javac greetings/Hi.java
% 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

% 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

Exercises

  1. Executed while in the cs50 directory that:
  2. Executed while in the cs50 directory that compiles: 
  3. Executed while in the cs50 directory that compiles: 
  4. Executed while in the cs50 directory that compiles: 
  5. Executed while in the cs50 directory that compiles: 
  6. Executed while in the cs50 directory that compiles, using the -sourcepath option
  7. Executed while in the cs50 directory that compiles, using the -sourcepath option: 
  8. Executed while in the cs50 directory that compiles, using the -sourcepath option: 
javac  -classpath class:library/junit.jar:jogl.jar \
       -sourcepath source:test \
       -d class \
       source/teamname/projectname/Main.java \
       test/teamname/projectname/MainTest.java
  1. Executed while in the smith directory that compiles, using the -sourcepath option: 
protect tree