import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;
public class Main {
public static void main(String[] args) throws Exception {
String sourceFile = "c:/HelloWorld.Java";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
List<File> sourceFileList = new ArrayList<File>();
sourceFileList.add(new File(sourceFile));
Iterable<? extends JavaFileObject> compilationUnits = fileManager
.getJavaFileObjectsFromFiles(sourceFileList);
CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
boolean result = task.call();
if (result) {
System.out.println("Compilation was successful");
} else {
System.out.println("Compilation failed");
}
fileManager.close();
}
}
|