Name

javac - Java programming language compiler

SYNOPSIS

f3
        f3javac [ options ] [ sourcefiles ] [ classes ] [ @argfiles ]
 

Arguments may be in any order.

options Command-line options.

sourcefiles One or more source files to be compiled (such as MyClass.java).

classes One or more classes to be processed for annotations (such as MyPackage.MyClass).

@argfiles One or more files that lists options and source files. The f2-J options are not allowed in these files.

DESCRIPTION

The f3javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. It can also process annotations in Java source files and classes.

There are two ways to pass source code file names to f3javac:

o For a small number of source files, simply list the file names on the command line.

o For a large number of source files, list the file names in a file, separated by blanks or line breaks. Then use the list file name on the f3javac command line, preceded by an f3@ character.

Source code file names must have f2.java suffixes, class file names must have f2.class suffixes, and both source and class files must have root names that identify the class. For example, a class called f2MyClass would be written in a source file called f2MyClass.java and compiled into a bytecode class file called f2MyClass.class.

Inner class definitions produce additional class files. These class files have names combining the inner and outer class names, such as f2MyClass$MyInnerClass.class.

You should arrange source files in a directory tree that reflects their package tree. For example, if you keep all your source files in f3/workspace, the source code for f2com.mysoft.mypack.MyClass should be in f3/workspace/com/mysoft/mypack/MyClass.java.

By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with f3-d (see Options, below).

OPTIONS

The compiler has a set of standard options that are supported on the current development environment and will be supported in future releases. An additional set of non-standard options are specific to the current virtual machine and compiler implementations and are subject to change in the future. Non-standard options begin with f3-X.

Standard Options

-Akey[=value] Options to pass to annotation processors. These are not interpreted by javac directly, but are made available for use by individual processors. f2key should be one or more identifiers separated by ".".

-cp path or -classpath path Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the f3CLASSPATH environment variable. If neither f3CLASSPATH, f3-cp nor f3-classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.

>If the f3-sourcepath option is not specified, the user class path is also searched for source files.

If the f3-processorpath option is not specified, the class path is also searched for annotation processors.

-Djava.ext.dirs=directories Override the location of installed extensions.

-Djava.endorsed.dirs=directories Override the location of endorsed standards path.

-d directory Set the destination directory for class files. The directory must already exist; f3javac will not create it. If a class is part of a package, f3javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify f3-d /home/myclasses and the class is called f2com.mypackage.MyClass, then the class file is called f2/home/myclasses/com/mypackage/MyClass.class.

If f3-d is not specified, f3javac puts each class files in the same directory as the source file from which it was generated.

f3Note: The directory specified by f3-d is not automatically added to your user class path.

-deprecation Show a description of each use or override of a deprecated member or class. Without f3-deprecation, f3javac shows a summary of the source files that use or override deprecated members or classes. f3-deprecation is shorthand for f3-Xlint:deprecation.

-encoding encoding Set the source file encoding name, such as f2EUC-JP and UTF-8. If f3-encoding is not specified, the platform default converter is used.

-endorseddirs directories Override the location of endorsed standards path.

-extdirs directories Overrides the location of the f2ext directory. The f2directories variable is a colon-separated list of directories. Each JAR archive in the specified directories is searched for class files. All JAR archives found are automatically part of the class path.

If you are cross-compiling (compiling classes against bootstrap and extension classes of a different Java platform implementation), this option specifies the directories that contain the extension classes. See Cross-Compilation Options for more information.

-g Generate all debugging information, including local variables. By default, only line number and source file information is generated.

-g:none Do not generate any debugging information.

-g:{keyword list} Generate only some kinds of debugging information, specified by a comma separated list of keywords. Valid keywords are:

source Source file debugging information

lines Line number debugging information

vars Local variable debugging information

-help Print a synopsis of standard options.

-implicit:{class,none} Controls the generation of class files for implicitly loaded source files. To automatically generate class files, use f3-implicit:class. To suppress class file generation, use f3-implicit:none. If this option is not specified, the default is to automatically generate class files. In this case, the compiler will issue a warning if any such class files are generated when also doing annotation processing. The warning will not be issued if this option is set explicitly. See Searching For Types.

-Joption Pass f2option to the f3java launcher called by f3javac. For example, f3-J-Xms48m sets the startup memory to 48 megabytes. It is a common convention for f3-J to pass options to the underlying VM executing applications written in Java.

f3Note: f3CLASSPATH, f3-classpath, f3-bootclasspath, and f3-extdirs do f2not specify the classes used to run f3javac. Fiddling with the implementation of the compiler in this way is usually pointless and always risky. If you do need to do this, use the f3-J option to pass through options to the underlying f3java launcher.

-nowarn Disable warning messages. This has the same meaning as f3-Xlint:none.

-proc: {none,only} Controls whether annotation processing and/or compilation is done. f3-proc:none means that compilation takes place without annotation processing. f3-proc:only means that only annotation processing is done, without any subsequent compilation.

-processor class1[,class2,class3...] Names of the annotation processors to run. This bypasses the default discovery process.

-processorpath path Specify where to find annotation processors; if this option is not used, the class path will be searched for processors.

-s dir Specify the directory where to place generated source files. The directory must already exist; f3javac will not create it. If a class is part of a package, the compiler puts the source file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify f3-s /home/mysrc and the class is called f2com.mypackage.MyClass, then the source file will be placed in f2/home/mysrc/com/mypackage/MyClass.java.

-source release Specifies the version of source code accepted. The following values for f2release are allowed:

1.3 The compiler does f2not support assertions, generics, or other language features introduced after Java SE 1.3.

1.4 The compiler accepts code containing assertions, which were introduced in Java SE 1.4.

1.5 The compiler accepts code containing generics and other language features introduced in Java SE 5.

5 Synonym for 1.5.

1.6 No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors instead of warnings as in previous releases of Java SE.

6 Synonym for 1.6.

1.7 This is the default value. The compiler accepts code with features introduced in Java SE 7.

7 Synonym for 1.7.

-sourcepath sourcepath Specify the source code path to search for class or interface definitions. As with the user class path, source path entries are separated by colons (f3:) 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.

f3Note: Classes found through the class path may be subject to automatic recompilation if their sources are also found. See Searching For Types.

-verbose Verbose output. This includes information about each class loaded and each source file compiled.

-version Print version information.

-Werror Terminate compilation if warnings occur.

-X Display information about non-standard options and exit.

Cross-Compilation Options

By default, classes are compiled against the bootstrap and extension classes of the platform that f3javac shipped with. But f3javac also supports f2cross-compiling, where classes are compiled against a bootstrap and extension classes of a different Java platform implementation. It is important to use f3-bootclasspath and f3-extdirs when cross-compiling; see Cross-Compilation Example below.

-target version Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are f31.1, f31.2, f31.3, f31.4, f31.5 (also f35), f31.6 (also f36), and f31.7 (also f37).

The default for f3-target depends on the value of f3-source:

o If -source is f3not specified, the value of -target is f31.7

o If -source is f31.2, the value of -target is f31.4

o If -source is f31.3, the value of -target is f31.4

o If -source is f31.5, the value of -target is f31.7

o If -source is f31.6, the value of -target is f31.7

o For f3all other values of -source, the value of f3-target is the value of f3-source.

-bootclasspath bootclasspath Cross-compile against the specified set of boot classes. As with the user class path, boot class path entries are separated by colons (f3:) and can be directories, JAR archives, or ZIP archives.

Non-Standard Options

-Xbootclasspath/p:path Prepend to the bootstrap class path.

-Xbootclasspath/a:path Append to the bootstrap class path.

-Xbootclasspath/:path Override location of bootstrap class files.

-Xlint Enable all recommended warnings. In this release, enabling all available warnings is recommended.

-Xlint:all Enable all recommended warnings. In this release, enabling all available warnings is recommended.

-Xlint:none Disable all warnings.

-Xlint:name Enable warning f2name. See the section Warnings That Can Be Enabled or Disabled with -Xlint Option for a list of warnings you can enable with this option.

-Xlint:-name Disable warning f2name. See the section Warnings That Can Be Enabled or Disabled with -Xlint Option for a list of warnings you can disable with this option.

-Xmaxerrs number Set the maximum number of errors to print.

-Xmaxwarns number Set the maximum number of warnings to print.

-Xstdout filename Send compiler messages to the named file. By default, compiler messages go to f2System.err.

-Xprefer:{newer,source} Specify which file to read when both a source file and class file are found for a type. (See Searching For Types). If f2-Xprefer:newer is used, it reads the newer of the source or class file for a type (default). If the f2-Xprefer:source option is used, it reads source file. Use f2-Xprefer:source when you want to be sure that any annotation processors can access annotations declared with a retention policy of f2SOURCE.

-Xpkginfo:{always,legacy,nonempty} Specify handling of package-info files

-Xprint Print out textual representation of specified types for debugging purposes; perform neither annotation processing nor compilation. The format of the output may change.

-XprintProcessorInfo Print information about which annotations a processor is asked to process.

-XprintRounds Print information about initial and subsequent annotation processing rounds.

Warnings That Can Be Enabled or Disabled with -Xlint Option

Enable warning f2name with the option f3-Xlint:f2name, where f2name is one of the following warning names. Similarly, you can disable warning f2name with the option f3-Xlint:-f2name:

cast Warn about unnecessary and redundant casts. For example:

f3
String s = (String)"Hello!"

classfile Warn about issues related to classfile contents.

deprecation Warn about use of deprecated items. For example:

f3
    java.util.Date myDate = new java.util.Date();
    int currentDay = myDate.getDay();

The method f2java.util.Date.getDay has been deprecated since JDK 1.1.

dep-ann Warn about items that are documented with an f2@deprecated Javadoc comment, but do not have a f2@Deprecated annotation. For example:

f3
  /**
   * @deprecated As of Java SE 7, replaced by {@link #newMethod()}
   */
 
  public static void deprecatedMethood() { }
 
  public static void newMethod() { }

divzero Warn about division by constant integer 0. For example:

f3
    int divideByZero = 42 / 0;

empty Warn about empty statements after f2if statements. For example:

f3
class E {
    void m() {
        if (true) ;
    }
}

fallthrough Check f2switch blocks for fall-through cases and provide a warning message for any that are found. Fall-through cases are cases in a f2switch block, other than the last case in the block, whose code does not include a f2break statement, allowing code execution to "fall through" from that case to the next case. For example, the code following the f2case 1 label in this f2switch block does not end with a f2break statement:

f3
switch (x) {
case 1:
       System.out.println("1");
       //  No break statement here.
case 2:
       System.out.println("2");
}

If the f2-Xlint:fallthrough flag were used when compiling this code, the compiler would emit a warning about "possible fall-through into case," along with the line number of the case in question.

finally Warn about f2finally clauses that cannot complete normally. For example:

f3
  public static int m() {
    try {
      throw new NullPointerException();
    } catch (NullPointerException e) {
      System.err.println("Caught NullPointerException.");
      return 1;
    } finally {
      return 0;
    }
  }

The compiler generates a warning for f2finally block in this example. When this method is called, it returns a value of f20, not f21. A f2finally block always executes when the f2try block exits. In this example, if control is transferred to the f2catch, then the method exits. However, the f2finally block must be executed, so it is executed, even though control has already been transferred outside the method.

options Warn about issues relating to the use of command line options. See Cross-Compilation Example for an example of this kind of warning.

overrides Warn about issues regarding method overrides. For example, consider the following two classes:

f3
public class ClassWithVarargsMethod {
  void varargsMethod(String... s) { }
}

f3
public class ClassWithOverridingMethod extends ClassWithVarargsMethod {
  @Override
  void varargsMethod(String[] s) { }
}

The compiler generates a warning similar to the following:

f2warning: [override] varargsMethod(String[]) in ClassWithOverridingMethod overrides varargsMethod(String...) in ClassWithVarargsMethod; overriding method is missing '...'

When the compiler encounters a varargs method, it translates the varargs formal parameter into an array. In the method f2ClassWithVarargsMethod.varargsMethod, the compiler translates the varargs formal parameter f2String... s to the formal parameter f2String[] s, an array, which matches the formal parameter of the method f2ClassWithOverridingMethod.varargsMethod. Consequently, this example compiles.

path Warn about invalid path elements and nonexistent path directories on the command line (with regards to the class path, the source path, and other paths). Such warnings cannot be suppressed with the f2@SuppressWarnings annotation. For example:

f3
javac -Xlint:path -classpath /nonexistentpath Example.java

processing Warn about issues regarding annotation processing. The compiler generates this warning if you have a class that has an annotation, and you use an annotation processor that cannot handle that type of exception. For example, the following is a simple annotation processor:

f3Source file f4AnnoProc.java:

f3
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.*;
import javax.lang.model.element.*;
 
@SupportedAnnotationTypes("NotAnno")
public class AnnoProc extends AbstractProcessor {
    public boolean process(Set<? extends TypeElement> elems, RoundEnvironment renv) {
        return true;
    }
 
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latest();
    }
}

f3Source file f4AnnosWithoutProcessors.javaf3:

f3
@interface Anno { }
 
@Anno
class AnnosWithoutProcessors { }

The following commands compile the annotation processor f2AnnoProc, then run this annotation processor against the source file f2AnnosWithoutProcessors.java:

f3
% f3javac AnnoProc.java
% f3javac -cp . -Xlint:processing -processor AnnoProc -proc:only AnnosWithoutProcessors.java

When the compiler runs the annotation processor against the source file f2AnnosWithoutProcessors.java, it generates the following warning:

f2warning: [processing] No processor claimed any of these annotations: Anno

To resolve this issue, you can rename the annotation defined and used in the class f2AnnosWithoutProcessors from f2Anno to f2NotAnno.

rawtypes Warn about unchecked operations on raw types. The following statement generates a f2rawtypes warning:

f3
void countElements(List l) { ... }

The following does not generate a f2rawtypes warning:

f3
void countElements(List<?> l) { ... }

f2List is a raw type. However, f2List<?> is a unbounded wildcard parameterized type. Because f2List is a parameterized interface, you should always specify its type argument. In this example, the f2List formal argument is specified with a unbounded wildcard (f2?) as its formal type parameter, which means that the f2countElements method can accept any instantiation of the f2List interface.

serial Warn about missing f2serialVersionUID definitions on serializable classes. For example:

f3
public class PersistentTime implements Serializable
{
  private Date time;
 
   public PersistentTime() {
     time = Calendar.getInstance().getTime();
   }
 
   public Date getTime() {
     return time;
   }
}

The compiler generates the following warning:

f2warning: [serial] serializable class PersistentTime has no definition of serialVersionUID

If a serializable class does not explicitly declare a field named f2serialVersionUID, then the serialization runtime will calculate a default f2serialVersionUID value for that class based on various aspects of the class, as described in the Java Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare f2serialVersionUID values because the default process of computing f2serialVersionUID vales is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected f2InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent f2serialVersionUID value across different Java compiler implementations, a serializable class must declare an explicit f2serialVersionUID value.

static Warn about issues relating to use of statics. For example:

f3
class XLintStatic {
    static void m1() { }
    void m2() { this.m1(); }
}

The compiler generates the following warning:

f3
warning: [static] static method should be qualified by type name, XLintStatic, instead of by an expression

To resolve this issue, you can call the static method f2m1 as follows:

f3
XLintStatic.m1();

Alternatively, you can remove the f2static keyword from the declaration of the method f2m1.

try Warn about issues relating to use of f2try blocks, including try-with-resources statements. For example, a warning is generated for the following statement because the resource f2ac declared in the f2try statement is not used:

f3
try ( AutoCloseable ac = getResource() ) {
    // do nothing
}

unchecked Give more detail for unchecked conversion warnings that are mandated by the Java Language Specification. For example:

f3
    List l = new ArrayList<Number>();
    List<String> ls = l;       // unchecked warning

During type erasure, the types f2ArrayList<Number> and f2List<String> become f2ArrayList and f2List, respectively.

The variable f2ls has the parameterized type f2List<String>. When the f2List referenced by f2l is assigned to f2ls, the compiler generates an unchecked warning; the compiler is unable to determine at compile time, and moreover knows that the JVM will not be able to determine at runtime, if f2l refers to a f2List<String> type; it does not. Consequently, heap pollution occurs.

In detail, a heap pollution situation occurs when the f2List object f2l, whose static type is f2List<Number>, is assigned to another f2List object, f2ls, that has a different static type, f2List<String>. However, the compiler still allows this assignment. It must allow this assignment to preserve backwards compatibility with versions of Java SE that do not support generics. Because of type erasure, f2List<Number> and f2List<String> both become f2List. Consequently, the compiler allows the assignment of the object f2l, which has a raw type of f2List, to the object f2ls.

varargs Warn about unsafe usages of variable arguments (varargs) methods, in particular, those that contain non-reifiable arguments. For example:

f3
public class ArrayBuilder {
  public static <T> void addToList (List<T> listArg, T... elements) {
    for (T x : elements) {
      listArg.add(x);
    }
  }
}

The compiler generates the following warning for the definition of the method f2ArrayBuilder.addToList:

f3
warning: [varargs] Possible heap pollution from parameterized vararg type T

When the compiler encounters a varargs method, it translates the varargs formal parameter into an array. However, the Java programming language does not permit the creation of arrays of parameterized types. In the method f2ArrayBuilder.addToList, the compiler translates the varargs formal parameter f2T... elements to the formal parameter f2T[] elements, an array. However, because of type erasure, the compiler converts the varargs formal parameter to f2Object[] elements. Consequently, there is a possibility of heap pollution.

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 f2javac command (except f2-J options). 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. If a filename contains embedded spaces, put the whole filename in double quotes.

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 f2*.java). Use of the 'f2@' character to recursively interpret files is not supported. The f2-J options are not supported because they are passed to the launcher, which does not support argument files.

When executing javac, pass in the path and name of each argument file with the 'f2@' leading character. When javac encounters an argument beginning with the character `f2@', it expands the contents of that file into the argument list.

Example - Single Arg File

You could use a single argument file named "f2argfile" to hold all javac arguments:

f3
% f3javac @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 "f2options" containing:

f3
     -d classes
     -g
     -sourcepath /java/pubs/ws/1.3/src/share/classes
 

Create a file named "f2classes" containing:

f3
     MyClass1.java
     MyClass2.java
     MyClass3.java
 

You would then run f3javac with:

f3
  % f3javac @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 f2path1 or f2path2):

f3
% f3javac @path1/options @path2/classes

ANNOTATION PROCESSING

f3javac provides direct support for annotation processing, superseding the need for the separate annotation processing tool, f3apt.

The API for annotation processors is defined in the f2javax.annotation.processing and f2javax.lang.model packages and subpackages.

Overview of annotation processing

Unless annotation processing is disabled with the f3-proc:none option, the compiler searches for any annotation processors that are available. The search path can be specified with the f3-processorpath option; if it is not given, the user class path is used. Processors are located by means of service provider-configuration files named f2META-INF/services/javax.annotation.processing.Processor on the search path. Such files should contain the names of any annotation processors to be used, listed one per line. Alternatively, processors can be specified explicitly, using the f3-processor option.

After scanning the source files and classes on the command line to determine what annotations are present, the compiler queries the processors to determine what annotations they process. When a match is found, the processor will be invoked. A processor may "claim" the annotations it processes, in which case no further attempt is made to find any processors for those annotations. Once all annotations have been claimed, the compiler does not look for additional processors.

If any processors generate any new source files, another round of annotation processing will occur: any newly generated source files will be scanned, and the annotations processed as before. Any processors invoked on previous rounds will also be invoked on all subsequent rounds. This continues until no new source files are generated.

After a round occurs where no new source files are generated, the annotation processors will be invoked one last time, to give them a chance to complete any work they may need to do. Finally, unless the f3-proc:only option is used, the compiler will compile the original and all the generated source files.

Implicitly loaded source files

To compile a set of source files, the compiler may need to implicitly load additional source files. (See Searching For Types). Such files are currently not subject to annotation processing. By default, the compiler will give a warning if annotation processing has occurred and any implicitly loaded source files are compiled. See the -implicit option for ways to suppress the warning.

SEARCHING FOR TYPES

When compiling a source file, the compiler often needs information about a type whose definition did not appear in the source files given on the command line. 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 f3java.applet.Applet, you are also using f3Applet's ancestor classes: f3java.awt.Panel, f3java.awt.Container, f3java.awt.Component, and f3java.lang.Object.

When the compiler needs type information, it looks for a source file or class file which defines the type. The compiler searches for class files first in the bootstrap and extension classes, then in the user class path (which by default is the current directory). The user class path is defined by setting the f3CLASSPATH environment variable or by using the f3-classpath command line option. (For details, see Setting the Class Path).

If you set the -sourcepath option, the compiler searches the indicated path for source files; otherwise the compiler searches the user class path for both class files and source files.

You can specify different bootstrap or extension classes with the f3-bootclasspath and f3-extdirs options; see Cross-Compilation Options below.

A successful type search may produce a class file, a source file, or both. If both are found, you can use the -Xprefer option to instruct the compiler which to use. If f3newer is given, the compiler will use the newer of the two files. If f3source is given, it will use the source file. The default is f3newer.

If a type search finds a source file for a required type, either by itself, or as a result of the setting for f3-Xprefer, the compiler will read the source file to get the information it needs. In addition, it will by default compile the source file as well. You can use the -implicit option to specify the behavior. If f3none is given, no class files will be generated for the source file. If f3class is given, class files will be generated for the source file.

The compiler may not discover the need for some type information until after annotation processing is complete. If the type information is found in a source file and no f3-implicit option is given, the compiler will give a warning that the file is being compiled without being subject to annotation processing. To disable the warning, either specify the file on the command line (so that it will be subject to annotation processing) or use the f3-implicit option to specify whether or not class files should be generated for such source files.

PROGRAMMATIC INTERFACE

f3javac supports the new Java Compiler API defined by the classes and interfaces in the f2javax.tools package.

Example

To perform a compilation using arguments as you would give on the command line, you can use the following:

f3
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
int rc = javac.run(null, null, null, args);

This will write any diagnostics to the standard output stream, and return the exit code that f3javac would give when invoked from the command line.

You can use other methods on the f2javax.tools.JavaCompiler interface to handle diagnostics, control where files are read from and written to, and so on.

Old Interface

f3Note: This API is retained for backwards compatibility only; all new code should use the Java Compiler API, described above.

The f2com.sun.tools.javac.Main class provides two static methods to invoke the compiler from a program:

f3
public static int compile(String[] args);
public static int compile(String[] args, PrintWriter out);

The f2args parameter represents any of the command line arguments that would normally be passed to the javac program and are outlined in the above Synopsis section.

The f2out parameter indicates where the compiler's diagnostic output is directed.

The return value is equivalent to the exit value from f3javac.

Note that all f3other classes and methods found in a package whose name starts with f2com.sun.tools.javac (informally known as sub-packages of f2com.sun.tools.javac) are strictly internal and subject to change at any time.

EXAMPLES

Compiling a Simple Program

One source file, f2Hello.java, defines a class called f3greetings.Hello. The f2greetings directory is the package directory both for the source file and the class file and is off 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 f3-d.

f3
% f3ls
greetings/
% f3ls greetings
Hello.java
% f3cat 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]);
        }
    }
}
% f3javac greetings/Hello.java
% f3ls greetings
Hello.class   Hello.java
% f3java 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 f2greetings.

f3
% f3ls
greetings/
% f3ls greetings
Aloha.java         GutenTag.java      Hello.java         Hi.java
% f3javac greetings/*.java
% f3ls 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:

f3
% f3pwd
/examples
% f3javac greetings/Hi.java

Since f2greetings.Hi refers to other classes in the f2greetings 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 f2/examples to the user class path. We can do this by setting f3CLASSPATH, but here we'll use the f3-classpath option.

f3
% f3javac -classpath /examples /examples/greetings/Hi.java

If we change f2greetings.Hi again, to use a banner utility, that utility also needs to be accessible through the user class path.

f3
% f3javac -classpath /examples:/lib/Banners.jar  
            /examples/greetings/Hi.java

To execute a class in f2greetings, we need access both to f2greetings and to the classes it uses.

f3
% f3java -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, especially on large projects. We use f3-d to indicate the separate class file destination. Since the source files are not in the user class path, we use f3-sourcepath to help the compiler find them.

f3
% f3ls
classes/  lib/      src/
% f3ls src
farewells/
% f3ls src/farewells
Base.java      GoodBye.java
% f3ls lib
Banners.jar
% f3ls classes
% f3javac -sourcepath src -classpath classes:lib/Banners.jar  
            src/farewells/GoodBye.java -d classes
% f3ls classes
farewells/
% f3ls classes/farewells
Base.class      GoodBye.class

f3Note: The compiler compiled f2src/farewells/Base.java, even though we didn't specify it on the command line. To trace automatic compiles, use the f3-verbose option.

Cross-Compilation Example

The following example uses f3javac to compile code that will run on a 1.6 VM.

f3
% f3javac -source 1.6 -target 1.6 -bootclasspath jdk1.6.0/lib/rt.jar  
            -extdirs "" OldCode.java

The f2-source 1.6 option specifies that version 1.6 (or 6) of the Java programming language be used to compile f2OldCode.java. The option f3-target 1.6 option ensures that the generated class files will be compatible with 1.6 VMs. Note that in most cases, the value of the f3-target option is the value of the f3-source option; in this example, you can omit the f3-target option.

You must specify the f3-bootclasspath option to specify the correct version of the bootstrap classes (the f2rt.jar library). If not, the compiler generates a warning:

f3
% f3javac -source 1.6 OldCode.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6

If you do not specify the correct version of bootstrap classes, the compiler will use the old language rules (in this example, it will use version 1.6 of the Java programming language) combined with the new bootstrap classes, which can result in class files that do not work on the older platform (in this case, Java SE 6) because reference to non-existent methods can get included.

SEE ALSO

o

f2The javac Guide @

http://docs.oracle.com/javase/7/docs/technotes/guides/javac/index.html

o java(1) - the Java Application Launcher

o jdb(1) - Java Application Debugger

o javah(1) - C Header and Stub File Generator

o javap(1) - Class File Disassembler

o javadoc(1) - API Documentation Generator

o jar(1) - JAR Archive Tool

o

f2The Java Extensions Framework @

http://docs.oracle.com/javase/7/docs/technotes/guides/extensions/index.html