1 /******************************************************************************* 2 * Copyright (c) 2008, 2019 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jdt.core.compiler.batch; 15 16 import java.io.PrintWriter; 17 18 import org.eclipse.jdt.core.compiler.CompilationProgress; 19 import org.eclipse.jdt.internal.compiler.batch.Main; 20 21 /** 22 * A public API for invoking the Eclipse Compiler for Java. E.g. 23 * <pre> 24 * BatchCompiler.compile("C:\\mySources\\X.java -d C:\\myOutput", new PrintWriter(System.out), new PrintWriter(System.err), null); 25 * </pre> 26 * 27 * @since 3.4 28 * @noinstantiate This class is not intended to be instantiated by clients. 29 */ 30 public final class BatchCompiler { 31 32 /** 33 * Invokes the Eclipse Compiler for Java with the given command line arguments, using the given writers 34 * to print messages, and reporting progress to the given compilation progress. Returns whether 35 * the compilation completed successfully. 36 * <p> 37 * Reasons for a compilation failing to complete successfully include:</p> 38 * <ul> 39 * <li>an error was reported</li> 40 * <li>a runtime exception occurred</li> 41 * <li>the compilation was canceled using the compilation progress</li> 42 * </ul> 43 * <p> 44 * The specification of the command line arguments is defined by running the batch compiler's help 45 * <pre>BatchCompiler.compile("-help", new PrintWriter(System.out), new PrintWriter(System.err), null);</pre> 46 * 47 * @param commandLine the command line arguments passed to the compiler 48 * @param outWriter the writer used to print standard messages 49 * @param errWriter the writer used to print error messages 50 * @param progress the object to report progress to and to provide cancellation, or <code>null</code> if no progress is needed 51 * @return whether the compilation completed successfully 52 */ compile(String commandLine, PrintWriter outWriter, PrintWriter errWriter, CompilationProgress progress)53 public static boolean compile(String commandLine, PrintWriter outWriter, PrintWriter errWriter, CompilationProgress progress) { 54 return compile(Main.tokenize(commandLine), outWriter, errWriter, progress); 55 } 56 57 /** 58 * Invokes the Eclipse Compiler for Java with the given command line arguments, using the given writers 59 * to print messages, and reporting progress to the given compilation progress. Returns whether 60 * the compilation completed successfully. 61 * <p> 62 * Reasons for a compilation failing to complete successfully include:</p> 63 * <ul> 64 * <li>an error was reported</li> 65 * <li>a runtime exception occurred</li> 66 * <li>the compilation was canceled using the compilation progress</li> 67 * </ul> 68 * <p> 69 * The specification of the command line arguments is defined by running the batch compiler's help 70 * <pre>BatchCompiler.compile("-help", new PrintWriter(System.out), new PrintWriter(System.err), null);</pre> 71 * <p> 72 * Note that a <code>true</code> returned value indicates that no errors were reported, no runtime exceptions 73 * occurred and that the compilation was not canceled. 74 * 75 * @param commandLineArguments the command line arguments passed to the compiler 76 * @param outWriter the writer used to print standard messages 77 * @param errWriter the writer used to print error messages 78 * @param progress the object to report progress to and to provide cancellation, or <code>null</code> if no progress is needed 79 * @return whether the compilation completed successfully 80 */ compile(String[] commandLineArguments, PrintWriter outWriter, PrintWriter errWriter, CompilationProgress progress)81 public static boolean compile(String[] commandLineArguments, PrintWriter outWriter, PrintWriter errWriter, CompilationProgress progress) { 82 return Main.compile(commandLineArguments, outWriter, errWriter, progress); 83 } 84 BatchCompiler()85 private BatchCompiler() { 86 // prevent instantiation 87 } 88 } 89