JDT Core / HowTo: Run the Batch Compiler 
Finding the batch compiler
The batch compiler class is located in the internal classes of the JDT/Core plugin. So it is in the jdtcore.jar file in the directory plugins/org.eclipse.jdt.core. The name of the class is org.eclipse.jdt.internal.compiler.batch.Main
Running the batch compiler
  • Using the main method.
  • Using the main method. The Main class has a main method. This is the classical way to invoke the batch compiler on a command-line.
    • For example on a command-line:

    • java -classpath jdtcore.jar org.eclipse.jdt.internal.compiler.batch.Main -classpath rt.jar A.java
    • For example in a java source:

    • org.eclipse.jdt.internal.compiler.batch.Main.main(new String[] {"-classpath", "rt.jar", "A.java"});
  • Using the static compile(String) method.
  • The compile(String) method is a convenient method to invoke the batch compiler in a java application.
    Instead of:
    • org.eclipse.jdt.internal.compiler.batch.Main.main(new String[] {"-classpath", "rt.jar", "A.java"});
    • you can simply write: org.eclipse.jdt.internal.compiler.batch.Main.compile("-classpath rt.jar A.java");
Which options are available?

With the yellow background, these are required options.
With the orange background, these are suggested options.

 
Name Usage
-help Display the help message
-version Display the build number of the compiler. This is very useful to report a bug.
-classpath <dir 1>;<dir 2>;...;<dir P> This is a list of directory or jar files used to compile the source files. There is no default classpath. So this option is always required to compile source files.
-d <dir 1>|none This is used to specify in which directory the generated .class files should be dumped. If it is omitted, no package directory structure is created.
If you don't want to generate .class files, use -d none.
-target 1.1|1.2 This specifies the classfile target setting. The possible value are 1.1 or 1.2, default is 1.1
-1.3 Set compliance level to 1.3 (default)
-1.4 Set compliance level to 1.4.
-source 1.3|1.4 This is used to enable the assertion support of the compiler. The possible value are: 1.3 or 1.4, default is 1.3 in -1.3 mode and 1.4 in -1.4 mode. In 1.4, assert is treated as a keyword.
-warn:
constructorName
|packageDefaultMethod
|deprecation
|maskedCatchBlocks
|unusedLocals
|unusedArguments
|unusedImports
|syntheticAccess
|assertIdentifier
Set warning level.
e.g. -warn:unusedLocals,deprecation
constructorName warn method with constructor name
packageDefaultMethod warn attempt to override package-default method
deprecation warn usage of deprecated type or member
maskedCatchBlocks warn hidden catch block
unusedLocals warn unused local variable
unusedArguments warn unused method argument
unusedImports When enabled, the compiler will issue an error or a warning for unused import reference
syntheticAccess warn when performing synthetic access for innerclass
assertIdentifier warn occurrence of assert used as identifier
-nowarn No warning (equivalent to -warn:none)
-deprecation Equivalent to -warn:deprecation.
-g[:none|:lines,vars,source] Set the debug attributes level
-g All debug info (equivalent to -g:lines,vars,source)
-g:none No debug info
-g:[lines,vars,source] Selective debug info
-preserveAllLocals Explicitly request the compiler to preserve all local variables (for debug purpose). If omitted, the compiler will removed unused locals.
-noImportError The compiler won't report an error for unresolved imports. A warning is issued instead.
-encoding <encoding name> Specify default source encoding format (custom encoding can also be specifed on a per file basis by suffixing each input source file/folder name with [encoding <encoding name>]).
-log <filename> Specify a log file in which all output from the compiler will be dumped. This is really useful if you want to debug the batch compiler or get a file which contains all errors and warnings from a batch build.
-proceedOnError Keep compiling when error, dumping class files with problem methods or problem types. This is recommanded only if you want to be able to run your application even if you have remaining errors.
-verbose Print accessed/processed compilation units in the console or the log file if specified.
-referenceInfo Compute reference info. This is useful only if connected to the builder. The reference infos are useless otherwise.
-progress Show progress (only in -log mode)
-time Display speed information
-noExit Do not call System.exit(n) at end of compilation (n=0 if no error)
-repeat <n> Repeat compilation process <n> times (perf analysis).
Examples
d:\temp -classpath rt.jar -time -g -d d:/tmp It compiles all source files in d:\temp and its subfolders. The classpath is simply rt.jar. It generates all debug attributes and all generated .class files are dumped in d:\tmp. The speed of the compiler will be displayed once the batch process is completed.
d:\temp\Test.java -classpath d:\temp;rt.jar -g:none It compiles only Test.java and it will retrieve any dependant files from d:\temp. The classpath is rt.jar and d:\temp, which means that all necessary classes are searched first in d:\temp and then in rt.jar. It generates no debug attributes and all generated .class files are dumped in d:\tmp.