## Javac

### Description

Compiles a source tree within the running (Ant) VM.

The source and destination directory will be recursively scanned for Java source files to compile. Only Java files that have no corresponding class file or where the class file is older than the java file will be compiled.

The directory structure of the source tree should follow the package hierarchy.

It is possible to refine the set of files that are being compiled/copied. This can be done with the includes, includesfile, excludes, excludesfile and defaultexcludes attributes. With the includes or includesfile attribute you specify the files you want to have included by using patterns. The exclude or excludesfile attribute is used to specify the files you want to have excluded. This is also done with patterns. And finally with the defaultexcludes attribute, you can specify whether you want to use default exclusions or not. See the section on directory based tasks, on how the inclusion/exclusion of files works, and how to write patterns.

It is possible to use different compilers. This can be selected with the "build.compiler" property. There are four choices:

- classic (the standard compiler of JDK 1.1/1.2)
- modern (the new compiler of JDK 1.3)
- jikes (the Jikes compiler)
- jvc (the Command-Line Compiler from Microsoft's SDK for Java / Visual J++)

For JDK 1.1/1.2, classic is the default. For JDK 1.3, modern is the default. If you wish to use a different compiler interface than one of the four supplied, write a class that implements the CompilerAdapter interface (package org.apache.tools.ant.taskdefs.compilers). Supply the full classname in the "build.compiler" property.

### Parameters

Attribute | Description | Required
srcdir | location of the java files. (See Notes at the end) | Yes, unless nested `<src>` elements are present.
destdir | location to store the class files. | No
includes | comma-separated list of patterns of files that must be included; all files are included when omitted. | No
includesfile | the name of a file that contains include patterns. | No
excludes | comma-separated list of patterns of files that must be excluded; no files (except default excludes) are excluded when omitted. | No
excludesfile | the name of a file that contains exclude patterns. | No
defaultexcludes | indicates whether default excludes should be used (`yes` | `no`); default excludes are used when omitted. | No
classpath | the classpath to use. | No
bootclasspath | location of bootstrap class files. | No
classpathref | the classpath to use, given as a reference to a PATH defined elsewhere. | No
bootclasspathref | location of bootstrap class files, given as a reference to a PATH defined elsewhere. | No
extdirs | location of installed extensions. | No
encoding | encoding of source files. | No
debug | indicates whether source should be compiled with debug information; defaults to `off`. | No
optimize | indicates whether source should be compiled with optimization; defaults to `off`. | No
deprecation | indicates whether source should be compiled with deprecation information; defaults to `off`. | No
target | generate class files for specific VM version (e.g., `1.1` or `1.2`). | No
verbose | asks the compiler for verbose output. | No
depend | enables dependency-tracking for compilers that support this (jikes and classic) | No
includeAntRuntime | whether to include the Ant run-time libraries; defaults to `yes`. | No
includeJavaRuntime | whether to include the default run-time libraries from the executing VM; defaults to `no`. | No
failonerror | indicates whether the build will continue even if there are compilation errors; defaults to `true`. | No

### Parameters specified as nested elements

This task forms an implicit FileSet and supports all attributes of `<fileset>` (`dir` becomes `srcdir`) as well as the nested `<include>`, `<exclude>` and `<patternset>` elements.

#### `src`, `classpath`, `bootclasspath` and `extdirs`

`Javac`'s srcdir, classpath, bootclasspath and extdirs attributes are path-like structures and can also be set via nested `<src>`, `<classpath>`, `<bootclasspath>` and `<extdirs>` elements, respectively.

### Examples

      <javac srcdir="${src}"
             destdir="${build}"
             classpath="xyz.jar"
             debug="on"
      />

compiles all `.java` files under the `${src}` directory, and stores the `.class` files in the `${build}` directory. The classpath used contains `xyz.jar`, and debug information is on.

      <javac srcdir="${src}"
             destdir="${build}"
             includes="mypackage/p1/**,mypackage/p2/**"
             excludes="mypackage/p1/testpackage/**"
             classpath="xyz.jar"
             debug="on"
      />

compiles `.java` files under the `${src}` directory, and stores the `.class` files in the `${build}` directory. The classpath used contains `xyz.jar`, and debug information is on. Only files under `mypackage/p1` and `mypackage/p2` are used. Files in the `mypackage/p1/testpackage` directory are excluded from compilation.

      <javac srcdir="${src}:${src2}"
             destdir="${build}"
             includes="mypackage/p1/**,mypackage/p2/**"
             excludes="mypackage/p1/testpackage/**"
             classpath="xyz.jar"
             debug="on"
      />

is the same as the previous example, with the addition of a second source path, defined by the property `src2`. This can also be represented using nested `<src>` elements as follows:

      <javac destdir="${build}"
             classpath="xyz.jar"
             debug="on">
        <src path="${src}"/>
        <src path="${src2}"/>
        <include name="mypackage/p1/**"/>
        <include name="mypackage/p2/**"/>
        <exclude name="mypackage/p1/testpackage/**"/>
      </javac>

Note: If you are using Ant on Windows and a new DOS window pops up for every use of an external compiler, this may be a problem of the JDK you are using. This problem may occur with all JDKs < 1.2.

Note: If you wish to compile only source-files located in some packages below a common root you should not include these packages in the srcdir-attribute. Use include/exclude-attributes or elements to filter for these packages. If you include part of your package-structure inside the srcdir-attribute (or nested src-elements) Ant will start to recompile your sources everytime you call it.

Copyright © 2000,2001 Apache Software Foundation. All rights Reserved.