## Compiling Java code

See the Java development user guide for using the batch compiler and using the ant javac adapter.

The JDT plug-ins include an incremental and batch Java compiler for building Java .class files from source code. There is no direct API provided by the compiler. It is installed as a builder on Java projects. Compilation is triggered using standard platform build mechanisms.

The platform build mechanism is described in detail in Incremental project builders.

### Compiling code

You can programmatically compile the Java source files in a project using the build API.

       IProject myProject;
       IProgressMonitor myProgressMonitor;
       myProject.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, myProgressMonitor);

For a Java project, this invokes the Java incremental project builder (along with any other incremental project builders that have been added to the project's build spec). The generated .class files are written to the designated output folder. Additional resource files are also copied to the output folder.

In the case of a full batch build, all the .class files in the output folder may be 'scrubbed' to ensure that no stale files are found. This is controlled using a JDT Core Builder Option (CORE_JAVA_BUILD_CLEAN_OUTPUT_FOLDER). The default for this option is to clean output folders. Unless this option is reset, you must ensure that you place all .class files for which you do not have corresponding source files in a separate class file folder on the classpath instead of the output folder.

The incremental and batch builders can be configured with other options that control which resources are copied to the output folder. The following sample shows how to set up a resource filter so that files ending with '.ignore' and folders named 'META-INF', are not copied to the output folder:

       Hashtable options = JavaCore.getOptions();
       options.put(JavaCore.CORE_JAVA_BUILD_RESOURCE_COPY_FILTER, "*.ignore,META-INF/");
       JavaCore.setOptions(options);

Filenames are filtered out if they match one of the supplied patterns. Entire folders are filtered out if their name matches one of the supplied folder names which end in a path separator.

The incremental and batch builders can also be configured to only generate a single error when the .classpath file has errors. This option is set by default and eliminates numerous errors. See JDT Core Builder Options for a complete list of builder-related options and their defaults.

The compiler can also be configured using JavaCore options. For example, you can define the severity that should be used for different kinds of problems that are found during compilation. See JDT Core Compiler Options for a complete list of compiler-related options and their defaults.

When programmatically configuring options for the builder or compiler, you should specify the scope of the option. For example, setting up a resource filter may apply to a particular project only:

       Hashtable options = myProject.getOptions(false);  // get only the options set up in this project
       options.put(JavaCore.CORE_JAVA_BUILD_RESOURCE_COPY_FILTER, "*.ignore,META-INF/");
       myProject.setOptions(options);

### Problem determination

JDT Core defines a specialized marker (marker type "org.eclipse.jdt.core.problem ") to denote compilation problems. To programmatically discover problems detected by the compiler, the standard platform marker protocol should be used. See Resource Markers for an overview of using markers.

The following snippet finds all Java problem markers in a compilation unit.

       public IMarker[] findJavaProblemMarkers(ICompilationUnit cu)
          throws CoreException {
          IResource javaSourceFile = cu.getUnderlyingResource();
          IMarker[] markers =
             javaSourceFile.findMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER,
                true, IResource.DEPTH_INFINITE);
       }

Java problem markers are maintained by the Java project builder and are removed automatically as problems are resolved and the Java source is recompiled.

The problem id value is set to one of the constants defined in IProblem . The problem's id is reliable, but the message is localized and therefore can be changed according to the default locale. The constants defined in IProblem are self-descriptive.

An implementation of IProblemRequestor should be defined to collect the problems discovered during a Java operation. Working copies can be reconciled with problem detection if a IProblemRequestor has been supplied for the working copy creation. To achieve this, you can use the reconcile method. Here is an example:

      ICompilationUnit unit = ..; // get some compilation unit

      // create requestor for accumulating discovered problems
      IProblemRequestor problemRequestor = new IProblemRequestor() {
        public void acceptProblem(IProblem problem) {
          System.out.println(problem.getID() + ": " + problem.getMessage());
        }
        public void beginReporting() {}
        public void endReporting() {}
        public boolean isActive() { return true; } // will detect problems if active
      };

      // use working copy to hold source with error
      ICompilationUnit workingCopy = unit.getWorkingCopy(new WorkingCopyOwner() {}, problemRequestor, null);
      ((IOpenable)workingCopy).getBuffer().setContents("public class X extends Zork {}");

      // trigger reconciliation
      workingCopy.reconcile(NO_AST, true, null, null);

You can add an action on the reported problems in the acceptProblem(IProblem) method. In this example, the reported problem will be that Zork cannot be resolved or is not a valid superclass and its id is IProblem.SuperclassNotFound.