List of usage examples for javax.tools StandardJavaFileManager close
@Override void close() throws IOException;
From source file:DiagnosticCollectorCompile.java
public static void main(String args[]) throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); Iterable<? extends JavaFileObject> compilationUnits = fileManager .getJavaFileObjectsFromStrings(Arrays.asList("Foo.java")); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits);/*from ww w. ja v a2 s. c o m*/ boolean success = task.call(); fileManager.close(); System.out.println("Success: " + success); }
From source file:Main.java
public static void main(String[] args) throws Exception { String sourceFile = "c:/HelloWorld.Java"; JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); 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); task.call();/* w ww . ja v a2s . c o m*/ fileManager.close(); List<Diagnostic<? extends JavaFileObject>> diagnosticList = diagnostics.getDiagnostics(); for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticList) { System.out.println("Position:" + diagnostic.getStartPosition()); } }
From source file:DiagnosticDemo.java
public static void main(String[] args) { String sourceFile = "c:/HelloWorld.Java"; JavaCompilerTool compiler = ToolProvider.getSystemJavaCompilerTool(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics); 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); task.run();// w ww .j a v a 2 s . c o m try { fileManager.close(); } catch (IOException e) { } List<Diagnostic<? extends JavaFileObject>> diagnosticList = diagnostics.getDiagnostics(); for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticList) { System.out.println("Position:" + diagnostic.getStartPosition()); } }
From source file:Main.java
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 {/*from w ww . j ava2s . c om*/ System.out.println("Compilation failed"); } fileManager.close(); }
From source file:JavaCompilerDemo.java
public static void main(String[] args) { String sourceFile = "c:/HelloWorld.Java"; JavaCompilerTool compiler = ToolProvider.getSystemJavaCompilerTool(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null); // prepare the source file(s) to compile 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); task.run();/*w w w . j a va 2 s . c om*/ boolean result = task.getResult(); if (result) { System.out.println("Compilation was successful"); } else { System.out.println("Compilation failed"); } try { fileManager.close(); } catch (IOException e) { } }
From source file:Main.java
public static void main(String args[]) throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); Iterable<? extends JavaFileObject> compilationUnits = fileManager .getJavaFileObjectsFromStrings(Arrays.asList("YourFile.java")); Iterable<String> options = Arrays.asList("-d", "classes", "-sourcepath", "src"); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);/*from w ww . j a va2s . c om*/ boolean success = task.call(); for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { System.out.println(diagnostic.getCode()); System.out.println(diagnostic.getKind()); System.out.println(diagnostic.getPosition()); System.out.println(diagnostic.getStartPosition()); System.out.println(diagnostic.getEndPosition()); System.out.println(diagnostic.getSource()); System.out.println(diagnostic.getMessage(null)); } fileManager.close(); System.out.println("Success: " + success); }
From source file:com.asakusafw.compiler.bootstrap.OperatorCompilerDriver.java
/** * ??// ww w. j a v a 2 s . c o m * @param sourcePath * @param outputPath ? * @param encoding * @param operatorClasses ? * @throws IOException ??????? * @throws IllegalArgumentException ?{@code null}???? */ public static void compile(File sourcePath, File outputPath, Charset encoding, List<Class<?>> operatorClasses) throws IOException { if (sourcePath == null) { throw new IllegalArgumentException("sourcePath must not be null"); //$NON-NLS-1$ } if (outputPath == null) { throw new IllegalArgumentException("outputPath must not be null"); //$NON-NLS-1$ } if (encoding == null) { throw new IllegalArgumentException("encoding must not be null"); //$NON-NLS-1$ } if (operatorClasses == null) { throw new IllegalArgumentException("operatorClasses must not be null"); //$NON-NLS-1$ } // ??JSR-199????? List<File> sourceFiles = toSources(sourcePath, operatorClasses); LOG.info(MessageFormat.format("Compiling {0}", sourceFiles)); List<String> arguments = toArguments(sourcePath, outputPath, encoding); LOG.debug("Compiler arguments {}", arguments); //$NON-NLS-1$ if (outputPath.isDirectory() == false && outputPath.mkdirs() == false) { throw new IOException(MessageFormat.format("Failed to create {0}", outputPath)); } JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if (compiler == null) { throw new IOException("Failed to create a compiler"); } StandardJavaFileManager files = compiler.getStandardFileManager(null, null, encoding); try { CompilationTask task = compiler.getTask(null, files, null, arguments, Collections.<String>emptyList(), files.getJavaFileObjectsFromFiles(sourceFiles)); if (task.call() == false) { LOG.error("Compilation Failed"); } } finally { files.close(); } LOG.info("Completed"); }
From source file:gov.va.vinci.leo.tools.AutoCompile.java
/** * Compile the files in the list. Optionally provide a destination directory where the * compiled files should be placed.//w ww .j a va2 s.com * * @param files List of java files to be compiled * @param outputDirectory Optional, output directory where the compiled files will be written * @throws Exception If there are errors compiling the files or we are unable to get a compiler */ public static void compileFiles(File[] files, String outputDirectory) throws Exception { //If the list is null there is nothing to do if (files == null) { return; } JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if (compiler == null) { throw new Exception("Unable to get a Compiler from the ToolProvider"); } StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, null, null); Iterable<String> compilationOptions = (StringUtils.isBlank(outputDirectory)) ? null : Arrays.asList(new String[] { "-d", outputDirectory }); try { Iterable<? extends JavaFileObject> compilationUnits = stdFileManager .getJavaFileObjectsFromFiles(Arrays.asList(files)); compiler.getTask(null, stdFileManager, null, compilationOptions, null, compilationUnits).call(); } finally { stdFileManager.close(); } //finally }
From source file:com.github.pellaton.jazoon2012.AnnotationProcessorTestCompiler.java
/** * Processes the java class specified. This implementation only parses and processes the java classes and does not * fully compile them - i.e. it does not write class files back to the disk. Basically, {@code javac} is called with * {@code -proc:only}./*from w w w.j a v a 2 s . com*/ * * @param classToCompile the Java class to compile * @param processor the annotation {@link Processor} to use during compilation * @return a list of {@link Diagnostic} messages emitted during the compilation */ public static List<Diagnostic<? extends JavaFileObject>> compileClass(String classToCompile, Processor processor) throws IOException { DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<JavaFileObject>(); StandardJavaFileManager fileManager = null; try { fileManager = getFileManager(collector); Iterable<? extends JavaFileObject> compilationUnits = getCompilationUnitOfClass(fileManager, classToCompile); CompilationTask task = COMPILER.getTask(null, fileManager, collector, COMPILER_OPTIONS, null, compilationUnits); task.setProcessors(Arrays.asList(processor)); task.call(); return collector.getDiagnostics(); } finally { if (fileManager != null) { fileManager.close(); } } }
From source file:de.xirp.ate.ATEManager.java
/** * Compiles the given Java {@link java.io.File} to the * corresponding byte code class files.//from w w w . j a v a2s .c om * * @param javaFile * The file containing the Java code. */ public static void compile(File javaFile) { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> compilationUnits = fileManager .getJavaFileObjectsFromFiles(Collections.singletonList(javaFile)); compiler.getTask(new PrintWriter(System.out), fileManager, null, null, null, compilationUnits).call(); try { fileManager.close(); } catch (IOException e) { logClass.error("Error: " + e.getMessage() //$NON-NLS-1$ + Constants.LINE_SEPARATOR, e); } }