List of usage examples for javax.tools ToolProvider getSystemJavaCompiler
public static JavaCompiler getSystemJavaCompiler()
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.ja v a 2 s . c o m System.out.println("Compilation failed"); } fileManager.close(); }
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();//from w w w . j a va 2s. 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:CompileSourceInMemory.java
public static void main(String args[]) throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); StringWriter writer = new StringWriter(); PrintWriter out = new PrintWriter(writer); out.println("public class HelloWorld {"); out.println(" public static void main(String args[]) {"); out.println(" System.out.println(\"This is in another java file\");"); out.println(" }"); out.println("}"); out.close();// w w w. j a v a 2s . c o m JavaFileObject file = new JavaSourceFromString("HelloWorld", writer.toString()); Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file); CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnits); 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)); } System.out.println("Success: " + success); if (success) { try { Class.forName("HelloWorld").getDeclaredMethod("main", new Class[] { String[].class }).invoke(null, new Object[] { null }); } catch (ClassNotFoundException e) { System.err.println("Class not found: " + e); } catch (NoSuchMethodException e) { System.err.println("No such method: " + e); } catch (IllegalAccessException e) { System.err.println("Illegal access: " + e); } catch (InvocationTargetException e) { System.err.println("Invocation target: " + e); } } }
From source file:gool.executor.ExecutorHelper.java
public static Iterable<? extends JavaFileObject> getJavaFileObjects(Collection<? extends File> inputFiles) { return ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null) .getJavaFileObjectsFromFiles(inputFiles); }
From source file:cop.raml.processor.AbstractProcessorTest.java
protected static boolean runAnnotationProcessor(File dir) throws URISyntaxException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); Set<JavaFileObject> compilationUnits = new LinkedHashSet<>(); for (File file : (List<File>) FileUtils.listFiles(dir, JAVA_FILE_FILTER, DirectoryFileFilter.INSTANCE)) compilationUnits.add(new SimpleJavaFileObjectImpl(file, JavaFileObject.Kind.SOURCE)); List<String> options = new ArrayList<>(); // options.put("-Apackages=com.bosch"); // options.put("-AfilterRegex=AnalysisController"); // options.put("-Aversion=0.8"); options.add("-d"); options.add("target"); JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, options, null, compilationUnits); task.setProcessors(Collections.singleton(new RestProcessor())); return task.call(); }
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./*from ww w . j a va 2 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.yahoo.maven.visitor.SuccessfulCompilationAsserter.java
public void assertNoCompilationErrors() throws IOException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, StandardCharsets.UTF_8)) { List<File> javaFiles = new ArrayList<>(); try (Stream<Path> stream = Files.walk(scratchSpace)) { Iterator<Path> iterator = stream.iterator(); while (iterator.hasNext()) { Path path = iterator.next(); if (Files.isRegularFile(path) && "java".equals(FilenameUtils.getExtension(path.toString()))) { javaFiles.add(path.toFile()); }/*w w w .ja v a 2 s . co m*/ } } Iterable<? extends JavaFileObject> compilationUnits = fileManager .getJavaFileObjectsFromFiles(javaFiles); compiler.getTask(null, fileManager, diagnostics, Arrays.asList("-classpath", System.getProperty("java.class.path")), null, compilationUnits) .call(); } int errors = 0; for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) { if (Diagnostic.Kind.ERROR.equals(diagnostic.getKind())) { System.err.println(diagnostic); errors++; } } assertEquals(0, errors); }
From source file:org.glowroot.common2.repo.util.Compilations.java
public static Class<?> compile(String source) throws Exception { JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<JavaFileObject>(); IsolatedClassLoader isolatedClassLoader = new IsolatedClassLoader(); StandardJavaFileManager standardFileManager = javaCompiler.getStandardFileManager(diagnosticCollector, Locale.ENGLISH, UTF_8); standardFileManager.setLocation(StandardLocation.CLASS_PATH, getCompilationClassPath()); JavaFileManager fileManager = new IsolatedJavaFileManager(standardFileManager, isolatedClassLoader); try {/*from w w w. j av a 2s . c om*/ List<JavaFileObject> compilationUnits = Lists.newArrayList(); String className = getPublicClassName(source); int index = className.lastIndexOf('.'); String simpleName; if (index == -1) { simpleName = className; } else { simpleName = className.substring(index + 1); } compilationUnits.add(new SourceJavaFileObject(simpleName, source)); JavaCompiler.CompilationTask task = javaCompiler.getTask(null, fileManager, diagnosticCollector, null, null, compilationUnits); task.call(); List<Diagnostic<? extends JavaFileObject>> diagnostics = diagnosticCollector.getDiagnostics(); if (!diagnostics.isEmpty()) { List<String> compilationErrors = Lists.newArrayList(); for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) { compilationErrors.add(checkNotNull(diagnostic.toString())); } throw new CompilationException(compilationErrors); } if (className.isEmpty()) { throw new CompilationException(ImmutableList.of("Class must be public")); } return isolatedClassLoader.loadClass(className); } finally { fileManager.close(); } }
From source file:com.googlecode.jsonschema2pojo.integration.util.Compiler.java
public void compile(File directory, String classpath) { JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> compilationUnits = fileManager .getJavaFileObjectsFromFiles(findAllSourceFiles(directory)); if (compilationUnits.iterator().hasNext()) { Boolean success = javaCompiler .getTask(null, fileManager, null, asList("-classpath", classpath), null, compilationUnits) .call();// ww w. j a v a2s .c o m assertThat("Compilation was not successful, check stdout for errors", success, is(true)); } }
From source file:org.cloudfoundry.practical.demo.local.LocalConfiguration.java
@Bean
JavaCompiler javaCompiler() {
return ToolProvider.getSystemJavaCompiler();
}