JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
OutputStream = outStream = ...;
OutputStream = errStream = ...;
int result = compilter.run(null, outStream, errStream, "-sourcepath", "src", "Test.java");
result 가 0이면 컴파일 성공
JavaCompilter.CompilationTask task = compiler.getTask(
errorWriter,
fileManager,
diagnostics,
options, // ex) Arrays.asList("-d", "bin")
classes,
sources
);
sources 얻기
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<JavaFileObject> sources = fileManager.getJavaFileObjectsFromFiles("File1.java", "File2.java");
JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, options, null, sources);
// 컴파일 시작
Boolean success = task.call();
public class StringSource extends SimpleJavaFileObject {
private String code;
StringSource(String name, String code) {
super(URI.create("string:///" + name.replace('.', '/') + ".java"), Kind.SOURCE);
this.code = code;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
}
String pointCode = ...;
String rectangleCode = ...;
List<StringSource> sources = Arrays.asList(
new StringSource("Point", pointCode),
new StringSource("Rectangle", rectangleCode)
);
task = compiler.getTask(null, null, null, null, null, sources);