List of usage examples for javax.tools StandardJavaFileManager getJavaFileObjectsFromStrings
Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names);
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);/* ww w .j a v a 2 s. com*/ boolean success = task.call(); fileManager.close(); System.out.println("Success: " + success); }
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 www.ja v a 2s. co m*/ 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.kelveden.rastajax.core.DynamicClassCompiler.java
private void compileFromFiles(final Set<String> sourceFilePaths) { final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); try {/*w ww . j a v a2 s. c o m*/ fileManager = compiler.getStandardFileManager(null, null, null); final Iterable<? extends JavaFileObject> fileObjects = fileManager .getJavaFileObjectsFromStrings(sourceFilePaths); if (!compiler.getTask(null, null, null, null, null, fileObjects).call()) { throw new RuntimeException("Failed to compile class."); } } finally { IOUtils.closeQuietly(fileManager); } }
From source file:gov.nih.nci.restgen.util.GeneratorUtil.java
public static boolean compileJavaSource(String srcFolder, String destFolder, String libFolder, GeneratorContext context) {/*from www . j a va 2 s . c o m*/ StandardJavaFileManager fileManager = null; boolean success = true; try { List<String> compilerFiles = GeneratorUtil.getFiles(srcFolder, new String[] { "java" }); GeneratorUtil.createOutputDir(destFolder); List<String> options = new ArrayList<String>(); options.add("-classpath"); String classPathStr = GeneratorUtil.getFiles(libFolder, new String[] { "jar" }, File.pathSeparator); options.add(classPathStr); options.add("-nowarn"); options.add("-Xlint:-unchecked"); options.add("-d"); options.add(destFolder); options.add("-s"); options.add(srcFolder); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); fileManager = compiler.getStandardFileManager(diagnostics, null, null); Iterable<? extends JavaFileObject> compilationUnits = fileManager .getJavaFileObjectsFromStrings(compilerFiles); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits); success = task.call(); for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { //context.getLogger().error(diagnostic.getCode()); //context.getLogger().error(diagnostic.getKind().toString()); //context.getLogger().error(diagnostic.getPosition() + ""); //context.getLogger().error(diagnostic.getStartPosition() + ""); //context.getLogger().error(diagnostic.getEndPosition() + ""); //context.getLogger().error(diagnostic.getSource().toString()); context.getLogger().error(diagnostic.toString()); } } catch (Throwable t) { context.getLogger().error("Error compiling java code", t); } finally { try { fileManager.close(); } catch (Throwable t) { } } return success; }
From source file:iristk.flow.FlowCompiler.java
public static void compileJavaFlow(File srcFile) throws FlowCompilerException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if (ToolProvider.getSystemJavaCompiler() == null) { throw new FlowCompilerException("Could not find Java Compiler"); }//w ww .j ava2 s. c o m if (!srcFile.exists()) { throw new FlowCompilerException(srcFile.getAbsolutePath() + " does not exist"); } File outputDir = new File(srcFile.getAbsolutePath().replaceFirst("([\\\\/])src[\\\\/].*", "$1") + "bin"); if (!outputDir.exists()) { throw new FlowCompilerException("Directory " + outputDir.getAbsolutePath() + " does not exist"); } DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, Locale.US, StandardCharsets.UTF_8); Iterable<? extends JavaFileObject> compilationUnits = fileManager .getJavaFileObjectsFromStrings(Arrays.asList(srcFile.getAbsolutePath())); Iterable<String> args = Arrays.asList("-d", outputDir.getAbsolutePath()); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, args, null, compilationUnits); boolean success = task.call(); try { fileManager.close(); } catch (IOException e) { } for (Diagnostic<? extends JavaFileObject> diag : diagnostics.getDiagnostics()) { if (diag.getKind() == Kind.ERROR) { int javaLine = (int) diag.getLineNumber(); int flowLine = mapLine(javaLine, srcFile); String message = diag.getMessage(Locale.US); message = message.replace("line " + javaLine, "line " + flowLine); throw new FlowCompilerException(message, flowLine); } } if (!success) { throw new FlowCompilerException("Compilation failed for unknown reason"); } }
From source file:com.cloudera.sqoop.orm.CompilationManager.java
/** * Compile the .java files into .class files via embedded javac call. * On success, move .java files to the code output dir. *///from www . ja v a 2 s .c om public void compile() throws IOException { List<String> args = new ArrayList<String>(); // ensure that the jar output dir exists. String jarOutDir = options.getJarOutputDir(); File jarOutDirObj = new File(jarOutDir); if (!jarOutDirObj.exists()) { boolean mkdirSuccess = jarOutDirObj.mkdirs(); if (!mkdirSuccess) { LOG.debug("Warning: Could not make directories for " + jarOutDir); } } else if (LOG.isDebugEnabled()) { LOG.debug("Found existing " + jarOutDir); } // Make sure jarOutDir ends with a '/'. if (!jarOutDir.endsWith(File.separator)) { jarOutDir = jarOutDir + File.separator; } // find hadoop-*-core.jar for classpath. String coreJar = findHadoopCoreJar(); if (null == coreJar) { // Couldn't find a core jar to insert into the CP for compilation. If, // however, we're running this from a unit test, then the path to the // .class files might be set via the hadoop.alt.classpath property // instead. Check there first. String coreClassesPath = System.getProperty("hadoop.alt.classpath"); if (null == coreClassesPath) { // no -- we're out of options. Fail. throw new IOException("Could not find hadoop core jar!"); } else { coreJar = coreClassesPath; } } // find sqoop jar for compilation classpath String sqoopJar = Jars.getSqoopJarPath(); if (null != sqoopJar) { sqoopJar = File.pathSeparator + sqoopJar; } else { LOG.warn("Could not find sqoop jar; child compilation may fail"); sqoopJar = ""; } String curClasspath = System.getProperty("java.class.path"); args.add("-sourcepath"); args.add(jarOutDir); args.add("-d"); args.add(jarOutDir); args.add("-classpath"); args.add(curClasspath + File.pathSeparator + coreJar + sqoopJar); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if (null == compiler) { LOG.error("It seems as though you are running sqoop with a JRE."); LOG.error("Sqoop requires a JDK that can compile Java code."); LOG.error("Please install a JDK and set $JAVA_HOME to use it."); throw new IOException("Could not start Java compiler."); } StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); ArrayList<String> srcFileNames = new ArrayList<String>(); for (String srcfile : sources) { srcFileNames.add(jarOutDir + srcfile); LOG.debug("Adding source file: " + jarOutDir + srcfile); } if (LOG.isDebugEnabled()) { LOG.debug("Invoking javac with args:"); for (String arg : args) { LOG.debug(" " + arg); } } Iterable<? extends JavaFileObject> srcFileObjs = fileManager.getJavaFileObjectsFromStrings(srcFileNames); JavaCompiler.CompilationTask task = compiler.getTask(null, // Write to stderr fileManager, null, // No special diagnostic handling args, null, // Compile all classes in the source compilation units srcFileObjs); boolean result = task.call(); if (!result) { throw new IOException("Error returned by javac"); } // Where we should move source files after compilation. String srcOutDir = new File(options.getCodeOutputDir()).getAbsolutePath(); if (!srcOutDir.endsWith(File.separator)) { srcOutDir = srcOutDir + File.separator; } // Move these files to the srcOutDir. for (String srcFileName : sources) { String orig = jarOutDir + srcFileName; String dest = srcOutDir + srcFileName; File fOrig = new File(orig); File fDest = new File(dest); File fDestParent = fDest.getParentFile(); if (null != fDestParent && !fDestParent.exists()) { if (!fDestParent.mkdirs()) { LOG.error("Could not make directory: " + fDestParent); } } if (!fOrig.renameTo(fDest)) { LOG.error("Could not rename " + orig + " to " + dest); } } }
From source file:gov.nih.nci.sdk.example.generator.WebServiceGenerator.java
private void compileWebServiceInterface() { java.util.Set<String> processedFocusDomainSet = (java.util.Set<String>) getScriptContext().getMemory() .get("processedFocusDomainSet"); if (processedFocusDomainSet == null) { processedFocusDomainSet = new java.util.HashSet<String>(); getScriptContext().getMemory().put("processedFocusDomainSet", processedFocusDomainSet); }/*from w ww. j a v a 2 s.c o m*/ processedFocusDomainSet.add(getScriptContext().getFocusDomain()); if (processedFocusDomainSet.containsAll(getScriptContext().retrieveDomainSet()) == true) { //All domains have been processed so now we can compile and generate WSDL StandardJavaFileManager fileManager = null; try { String jaxbPojoPath = GeneratorUtil.getJaxbPojoPath(getScriptContext()); String servicePath = GeneratorUtil.getServicePath(getScriptContext()); String serviceImplPath = GeneratorUtil.getServiceImplPath(getScriptContext()); String projectRoot = getScriptContext().getProperties().getProperty("PROJECT_ROOT"); List<String> compilerFiles = GeneratorUtil.getFiles(jaxbPojoPath, new String[] { "java" }); compilerFiles.addAll(GeneratorUtil.getFiles(servicePath, new String[] { "java" })); compilerFiles.addAll(GeneratorUtil.getFiles(serviceImplPath, new String[] { "java" })); getScriptContext().logInfo("Compiling files: " + compilerFiles); // Check if output directory exist, create it GeneratorUtil.createOutputDir(projectRoot + File.separator + "classes"); List<String> options = new ArrayList<String>(); options.add("-classpath"); String classPathStr = GeneratorUtil .getFiles(new java.io.File(getScriptContext().getGeneratorBase()).getAbsolutePath() + File.separator + "lib", new String[] { "jar" }, File.pathSeparator) + File.pathSeparator + new java.io.File(projectRoot + File.separatorChar + "classes").getAbsolutePath(); getScriptContext().logInfo("compiler classpath is: " + classPathStr); options.add(classPathStr); options.add("-d"); options.add(projectRoot + File.separator + "classes"); options.add("-s"); options.add(projectRoot + File.separator + "src/generated"); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); fileManager = compiler.getStandardFileManager(diagnostics, null, null); Iterable<? extends JavaFileObject> compilationUnits = fileManager .getJavaFileObjectsFromStrings(compilerFiles); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits); boolean success = task.call(); for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { getScriptContext().logInfo(diagnostic.getCode()); getScriptContext().logInfo(diagnostic.getKind().toString()); getScriptContext().logInfo(diagnostic.getPosition() + ""); getScriptContext().logInfo(diagnostic.getStartPosition() + ""); getScriptContext().logInfo(diagnostic.getEndPosition() + ""); getScriptContext().logInfo(diagnostic.getSource().toString()); getScriptContext().logInfo(diagnostic.getMessage(null)); } } catch (Throwable t) { getScriptContext().logError(t); } finally { try { fileManager.close(); } catch (Throwable t) { } } for (String focusDomain : getScriptContext().retrieveDomainSet()) { generateWebServiceArtifacts(focusDomain); } } }
From source file:org.apache.sqoop.orm.CompilationManager.java
/** * Compile the .java files into .class files via embedded javac call. * On success, move .java files to the code output dir. */// w ww. ja va 2 s .co m public void compile() throws IOException { List<String> args = new ArrayList<String>(); // ensure that the jar output dir exists. String jarOutDir = options.getJarOutputDir(); File jarOutDirObj = new File(jarOutDir); if (!jarOutDirObj.exists()) { boolean mkdirSuccess = jarOutDirObj.mkdirs(); if (!mkdirSuccess) { LOG.debug("Warning: Could not make directories for " + jarOutDir); } } else if (LOG.isDebugEnabled()) { LOG.debug("Found existing " + jarOutDir); } // Make sure jarOutDir ends with a '/'. if (!jarOutDir.endsWith(File.separator)) { jarOutDir = jarOutDir + File.separator; } // find hadoop-*-core.jar for classpath. String coreJar = findHadoopCoreJar(); if (null == coreJar) { // Couldn't find a core jar to insert into the CP for compilation. If, // however, we're running this from a unit test, then the path to the // .class files might be set via the hadoop.alt.classpath property // instead. Check there first. String coreClassesPath = System.getProperty("hadoop.alt.classpath"); if (null == coreClassesPath) { // no -- we're out of options. Fail. throw new IOException("Could not find hadoop core jar!"); } else { coreJar = coreClassesPath; } } // find sqoop jar for compilation classpath String sqoopJar = Jars.getSqoopJarPath(); if (null != sqoopJar) { sqoopJar = File.pathSeparator + sqoopJar; } else { LOG.warn("Could not find sqoop jar; child compilation may fail"); sqoopJar = ""; } String curClasspath = System.getProperty("java.class.path"); args.add("-sourcepath"); args.add(jarOutDir); args.add("-d"); args.add(jarOutDir); args.add("-classpath"); args.add(curClasspath + File.pathSeparator + coreJar + sqoopJar); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if (null == compiler) { LOG.error("It seems as though you are running sqoop with a JRE."); LOG.error("Sqoop requires a JDK that can compile Java code."); LOG.error("Please install a JDK and set $JAVA_HOME to use it."); throw new IOException("Could not start Java compiler."); } StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); ArrayList<String> srcFileNames = new ArrayList<String>(); for (String srcfile : sources) { srcFileNames.add(jarOutDir + srcfile); LOG.debug("Adding source file: " + jarOutDir + srcfile); } if (LOG.isDebugEnabled()) { LOG.debug("Invoking javac with args:"); for (String arg : args) { LOG.debug(" " + arg); } } Iterable<? extends JavaFileObject> srcFileObjs = fileManager.getJavaFileObjectsFromStrings(srcFileNames); JavaCompiler.CompilationTask task = compiler.getTask(null, // Write to stderr fileManager, null, // No special diagnostic handling args, null, // Compile all classes in the source compilation units srcFileObjs); boolean result = task.call(); if (!result) { throw new IOException("Error returned by javac"); } // Where we should move source files after compilation. String srcOutDir = new File(options.getCodeOutputDir()).getAbsolutePath(); if (!srcOutDir.endsWith(File.separator)) { srcOutDir = srcOutDir + File.separator; } // Move these files to the srcOutDir. for (String srcFileName : sources) { String orig = jarOutDir + srcFileName; String dest = srcOutDir + srcFileName; File fOrig = new File(orig); File fDest = new File(dest); File fDestParent = fDest.getParentFile(); if (null != fDestParent && !fDestParent.exists()) { if (!fDestParent.mkdirs()) { LOG.error("Could not make directory: " + fDestParent); } } try { FileUtils.moveFile(fOrig, fDest); } catch (IOException e) { LOG.debug("Could not rename " + orig + " to " + dest, e); } } }