Java by API/javax.tools/StandardJavaFileManager — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 20:43, 31 мая 2010
JavaFileManager: close()
<source lang="java">
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(); }
}
</source>
StandardJavaFileManager: getJavaFileObjectsFromFiles(Iterable<? extends File> arg0)
<source lang="java">
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(); }
}
</source>