Java by API/javax.tools/JavaCompiler
Содержание
- 1 CompilationTask: call()
- 2 JavaCompiler: getStandardFileManager(DiagnosticListener<? super JavaFileObject> arg0, Locale arg1, Charset arg2)
- 3 JavaCompiler: getTask(Writer arg0, JavaFileManager arg1, DiagnosticListener<? super JavaFileObject> arg2, Iterable<String> arg3, Iterable<String> arg4, Iterable<? extends JavaFileObject> arg5)
- 4 JavaCompiler: isSupportedOption(String option)
- 5 Tool: run(InputStream in, OutputStream out, OutputStream err, String... arguments)
CompilationTask: call()
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.rupilationTask;
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();
}
}
JavaCompiler: getStandardFileManager(DiagnosticListener<? super JavaFileObject> arg0, Locale arg1, Charset arg2)
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.rupilationTask;
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();
}
}
JavaCompiler: getTask(Writer arg0, JavaFileManager arg1, DiagnosticListener<? super JavaFileObject> arg2, Iterable<String> arg3, Iterable<String> arg4, Iterable<? extends JavaFileObject> arg5)
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.rupilationTask;
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();
}
}
JavaCompiler: isSupportedOption(String option)
import java.util.Set;
import javax.lang.model.SourceVersion;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class Main {
public static void main(String[] args) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
Set<SourceVersion> srcVer = compiler.getSourceVersions();
for (SourceVersion sv : srcVer)
System.out.println(" " + sv.name());
int nargs = compiler.isSupportedOption(args[0]);
if (nargs == -1)
System.out.println("Option " + args[0] + " is not supported");
else
System.out.println("Option " + args[0] + " takes " + nargs + " arguments");
}
}
Tool: run(InputStream in, OutputStream out, OutputStream err, String... arguments)
import java.io.IOException;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class Main {
public static void main(String args[]) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int results = compiler.run(null, null, null, "Foo.java");
System.out.println("Success: " + (results == 0));
}
}