List of usage examples for javax.tools JavaCompiler getTask
CompilationTask getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits);
From source file:net.cliseau.composer.javacor.MissingToolException.java
/** * Compiles the unit startup file from Java source to Java bytecode. * * This compiles the startup file. During this process, multiple class files * may be generated even though only a single input file to compile is * specified. The reason for this is that classes other than the main class * may be defined in the single file and are written to distinct class * files. All created files are collected and returned by the method. * * @param startupFile The file to be compiled. * @param startupDependencies Names of classpath entries to use for compilation. * @return List of names of created (class) files during compilation. * @exception UnitGenerationException Thrown when finding a Java compiler failed. *//* w w w . ja v a 2 s.c o m*/ private LinkedList<String> compile(final File startupFile, final Collection<String> startupDependencies) throws MissingToolException, InvalidConfigurationException { // code inspired by the examples at // http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html final LinkedList<String> createdFiles = new LinkedList<String>(); // set the file manager, which records the written files JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if (compiler == null) { throw new MissingToolException("Could not find system Java compiler."); } StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, null, null); JavaFileManager fileManager = new ForwardingJavaFileManager<StandardJavaFileManager>(stdFileManager) { /** * Collect the list of all output (class) files. * * Besides its side-effect on the createdFiles list of the containing * method, this method is functionally equivalent to its superclass * version. */ public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException { JavaFileObject fileForOutput = super.getJavaFileForOutput(location, className, kind, sibling); createdFiles.addLast(fileForOutput.getName()); return fileForOutput; } }; // set the files to compile Iterable<? extends JavaFileObject> compilationUnits = stdFileManager .getJavaFileObjectsFromFiles(Arrays.asList(startupFile)); // do the actual compilation ArrayList<String> compileParams = new ArrayList<String>(2); boolean verbose = org.apache.log4j.Level.DEBUG.isGreaterOrEqual(config.getInstantiationLogLevel()); if (verbose) compileParams.add("-verbose"); compileParams .addAll(Arrays.asList("-classpath", StringUtils.join(startupDependencies, File.pathSeparator))); if (!compiler.getTask(null, fileManager, null, compileParams, null, compilationUnits).call()) { // could not compile all files without error //TODO: throw an exception ... see where to get the required information from } return createdFiles; }
From source file:org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl.java
protected boolean compile(JavaCompiler compiler, ClassLoader parentClassLoader, CompilationUnit compilationUnit, String... options) {//from ww w. jav a 2 s . c o m if (compiler == null) { throw new IllegalStateException( "Failed to create the system Java compiler. Check that your class path includes tools.jar"); } JavaFileObjectRegistry registry = compilationUnit.getRegistry(); SimpleClassLoader result = new SimpleClassLoader(parentClassLoader, registry, compilationUnit.getOutputClassDirectory()); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); JavaFileManager standardFileManager = compiler.getStandardFileManager(diagnostics, null, null); JavaFileManager javaFileManager = new SimpleJavaFileManager(standardFileManager, result, registry); Iterable<JavaFileObject> sources = registry.get(JavaFileObject.Kind.SOURCE); Collection<String> compilationOptions = buildOptions(compilationUnit, result, options); JavaCompiler.CompilationTask task = compiler.getTask(null, javaFileManager, diagnostics, compilationOptions, null, sources); task.call(); if (getDiagnosticCountByType(diagnostics, Diagnostic.Kind.ERROR) > 0) { String diagnosticString = getDiagnosticString(registry, diagnostics); if (diagnosticString.contains("missing return statement")) { return false; } throw new IllegalStateException(diagnosticString); } if (getDiagnosticCountByType(diagnostics, Diagnostic.Kind.WARNING) > 0) { logger.warn(getDiagnosticString(registry, diagnostics)); } result.addClassPathEntries(compilationUnit.getClassPathsEntries()); return true; }
From source file:org.apache.hadoop.hbase.util.ClassLoaderTestHelper.java
/** * Create a test jar for testing purpose for a given class * name with specified code string./*from w w w . ja v a 2s . c o m*/ * * @param testDir the folder under which to store the test class * @param className the test class name * @param code the optional test class code, which can be null. * If null, an empty class will be used * @param folder the folder under which to store the generated jar * @return the test jar file generated */ public static File buildJar(String testDir, String className, String code, String folder) throws Exception { String javaCode = code != null ? code : "public class " + className + " {}"; Path srcDir = new Path(testDir, "src"); File srcDirPath = new File(srcDir.toString()); srcDirPath.mkdirs(); File sourceCodeFile = new File(srcDir.toString(), className + ".java"); BufferedWriter bw = new BufferedWriter(new FileWriter(sourceCodeFile)); bw.write(javaCode); bw.close(); // compile it by JavaCompiler JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); ArrayList<String> srcFileNames = new ArrayList<String>(); srcFileNames.add(sourceCodeFile.toString()); StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> cu = fm.getJavaFileObjects(sourceCodeFile); List<String> options = new ArrayList<String>(); options.add("-classpath"); // only add hbase classes to classpath. This is a little bit tricky: assume // the classpath is {hbaseSrc}/target/classes. String currentDir = new File(".").getAbsolutePath(); String classpath = currentDir + File.separator + "target" + File.separator + "classes" + System.getProperty("path.separator") + System.getProperty("java.class.path") + System.getProperty("path.separator") + System.getProperty("surefire.test.class.path"); options.add(classpath); LOG.debug("Setting classpath to: " + classpath); JavaCompiler.CompilationTask task = compiler.getTask(null, fm, null, options, null, cu); assertTrue("Compile file " + sourceCodeFile + " failed.", task.call()); // build a jar file by the classes files String jarFileName = className + ".jar"; File jarFile = new File(folder, jarFileName); jarFile.getParentFile().mkdirs(); if (!createJarArchive(jarFile, new File[] { new File(srcDir.toString(), className + ".class") })) { assertTrue("Build jar file failed.", false); } return jarFile; }
From source file:org.apache.pig.test.TestJobControlCompiler.java
/** * creates a jar containing a UDF not in the current classloader * @param jarFile the jar to create/* w w w . j a v a 2 s . c o m*/ * @return the name of the class created (in the default package) * @throws IOException * @throws FileNotFoundException */ private String createTestJar(File jarFile) throws IOException, FileNotFoundException { // creating the source .java file File javaFile = File.createTempFile("TestUDF", ".java"); javaFile.deleteOnExit(); String className = javaFile.getName().substring(0, javaFile.getName().lastIndexOf('.')); FileWriter fw = new FileWriter(javaFile); try { fw.write("import org.apache.pig.EvalFunc;\n"); fw.write("import org.apache.pig.data.Tuple;\n"); fw.write("import java.io.IOException;\n"); fw.write("public class " + className + " extends EvalFunc<String> {\n"); fw.write(" public String exec(Tuple input) throws IOException {\n"); fw.write(" return \"test\";\n"); fw.write(" }\n"); fw.write("}\n"); } finally { fw.close(); } // compiling it JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjects(javaFile); CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits1); task.call(); // here is the compiled file File classFile = new File(javaFile.getParentFile(), className + ".class"); Assert.assertTrue(classFile.exists()); // putting it in the jar JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile)); try { jos.putNextEntry(new ZipEntry(classFile.getName())); try { InputStream testClassContentIS = new FileInputStream(classFile); try { byte[] buffer = new byte[64000]; int n; while ((n = testClassContentIS.read(buffer)) != -1) { jos.write(buffer, 0, n); } } finally { testClassContentIS.close(); } } finally { jos.closeEntry(); } } finally { jos.close(); } return className; }
From source file:org.apache.pig.test.TestRegisteredJarVisibility.java
private static List<File> compile(File[] javaFiles) { LOG.info("Compiling: " + Arrays.asList(javaFiles)); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjects(javaFiles); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits1);// w w w . ja va 2 s . com task.call(); List<File> classFiles = Lists.newArrayList(); for (File javaFile : javaFiles) { File classFile = new File(javaFile.getAbsolutePath().replace(".java", ".class")); classFile.deleteOnExit(); Assert.assertTrue(classFile.exists()); classFiles.add(classFile); LOG.info("Created " + classFile.getAbsolutePath()); } return classFiles; }
From source file:org.apache.sqoop.integration.connectorloading.ClasspathTest.java
private Map<String, String> buildJar(String outputDir, Map<String, JarContents> sourceFileToJarMap) throws Exception { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); List<File> sourceFiles = new ArrayList<>(); for (JarContents jarContents : sourceFileToJarMap.values()) { sourceFiles.addAll(jarContents.sourceFiles); }//from w w w . j a v a 2 s . c om fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(outputDir.toString()))); Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(sourceFiles); boolean compiled = compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call(); if (!compiled) { throw new RuntimeException("failed to compile"); } for (Map.Entry<String, JarContents> jarNameAndContents : sourceFileToJarMap.entrySet()) { Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, "."); JarOutputStream target = new JarOutputStream( new FileOutputStream(outputDir.toString() + File.separator + jarNameAndContents.getKey()), manifest); List<String> classesForJar = new ArrayList<>(); for (File sourceFile : jarNameAndContents.getValue().getSourceFiles()) { //split the file on dot to get the filename from FILENAME.java String fileName = sourceFile.getName().split("\\.")[0]; classesForJar.add(fileName); } File dir = new File(outputDir); File[] directoryListing = dir.listFiles(); for (File compiledClass : directoryListing) { String classFileName = compiledClass.getName().split("\\$")[0].split("\\.")[0]; if (classesForJar.contains(classFileName)) { addFileToJar(compiledClass, target); } } for (File propertiesFile : jarNameAndContents.getValue().getProperitesFiles()) { addFileToJar(propertiesFile, target); } target.close(); } //delete non jar files File dir = new File(outputDir); File[] directoryListing = dir.listFiles(); for (File file : directoryListing) { String extension = file.getName().split("\\.")[1]; if (!extension.equals("jar")) { file.delete(); } } Map<String, String> jarMap = new HashMap<>(); jarMap.put(TEST_CONNECTOR_JAR_NAME, outputDir.toString() + File.separator + TEST_CONNECTOR_JAR_NAME); jarMap.put(TEST_DEPENDENCY_JAR_NAME, outputDir.toString() + File.separator + TEST_DEPENDENCY_JAR_NAME); return jarMap; }
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 w w . j a v a 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); } } }
From source file:org.apache.struts2.JSPLoader.java
/** * Compiles the given source code into java bytecode */// w w w . j a v a 2s. c om private void compileJava(String className, final String source, Set<String> extraClassPath) throws IOException { if (LOG.isTraceEnabled()) LOG.trace("Compiling [#0], source: [#1]", className, source); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); //the generated bytecode is fed to the class loader JavaFileManager jfm = new ForwardingJavaFileManager<StandardJavaFileManager>( compiler.getStandardFileManager(diagnostics, null, null)) { @Override public JavaFileObject getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject sibling) throws IOException { MemoryJavaFileObject fileObject = new MemoryJavaFileObject(name, kind); classLoader.addMemoryJavaFileObject(name, fileObject); return fileObject; } }; //read java source code from memory String fileName = className.replace('.', '/') + ".java"; SimpleJavaFileObject sourceCodeObject = new SimpleJavaFileObject(toURI(fileName), JavaFileObject.Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException, IllegalStateException, UnsupportedOperationException { return source; } }; //build classpath //some entries will be added multiple times, hence the set List<String> optionList = new ArrayList<String>(); Set<String> classPath = new HashSet<String>(); FileManager fileManager = ServletActionContext.getContext().getInstance(FileManagerFactory.class) .getFileManager(); //find available jars ClassLoaderInterface classLoaderInterface = getClassLoaderInterface(); UrlSet urlSet = new UrlSet(classLoaderInterface); //find jars List<URL> urls = urlSet.getUrls(); for (URL url : urls) { URL normalizedUrl = fileManager.normalizeToFileProtocol(url); File file = FileUtils.toFile(ObjectUtils.defaultIfNull(normalizedUrl, url)); if (file.exists()) classPath.add(file.getAbsolutePath()); } //these should be in the list already, but I am feeling paranoid //this jar classPath.add(getJarUrl(EmbeddedJSPResult.class)); //servlet api classPath.add(getJarUrl(Servlet.class)); //jsp api classPath.add(getJarUrl(JspPage.class)); try { Class annotationsProcessor = Class.forName("org.apache.AnnotationProcessor"); classPath.add(getJarUrl(annotationsProcessor)); } catch (ClassNotFoundException e) { //ok ignore } //add extra classpath entries (jars where tlds were found will be here) for (Iterator<String> iterator = extraClassPath.iterator(); iterator.hasNext();) { String entry = iterator.next(); classPath.add(entry); } String classPathString = StringUtils.join(classPath, File.pathSeparator); if (LOG.isDebugEnabled()) { LOG.debug("Compiling [#0] with classpath [#1]", className, classPathString); } optionList.addAll(Arrays.asList("-classpath", classPathString)); //compile JavaCompiler.CompilationTask task = compiler.getTask(null, jfm, diagnostics, optionList, null, Arrays.asList(sourceCodeObject)); if (!task.call()) { throw new StrutsException("Compilation failed:" + diagnostics.getDiagnostics().get(0).toString()); } }
From source file:org.apache.sysml.runtime.codegen.CodegenUtils.java
private static Class<?> compileClassJavac(String name, String src) { try {//w ww . j a v a2s .c om //create working dir on demand if (_workingDir == null) createWorkingDir(); //write input file (for debugging / classpath handling) File ftmp = new File(_workingDir + "/" + name.replace(".", "/") + ".java"); if (!ftmp.getParentFile().exists()) ftmp.getParentFile().mkdirs(); LocalFileUtils.writeTextFile(ftmp, src); //get system java compiler JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if (compiler == null) throw new RuntimeException("Unable to obtain system java compiler."); //prepare file manager DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); //prepare input source code Iterable<? extends JavaFileObject> sources = fileManager .getJavaFileObjectsFromFiles(Arrays.asList(ftmp)); //prepare class path URL runDir = CodegenUtils.class.getProtectionDomain().getCodeSource().getLocation(); String classpath = System.getProperty("java.class.path") + File.pathSeparator + runDir.getPath(); List<String> options = Arrays.asList("-classpath", classpath); //compile source code CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, sources); Boolean success = task.call(); //output diagnostics and error handling for (Diagnostic<? extends JavaFileObject> tmp : diagnostics.getDiagnostics()) if (tmp.getKind() == Kind.ERROR) System.err.println("ERROR: " + tmp.toString()); if (success == null || !success) throw new RuntimeException("Failed to compile class " + name); //dynamically load compiled class URLClassLoader classLoader = null; try { classLoader = new URLClassLoader(new URL[] { new File(_workingDir).toURI().toURL(), runDir }, CodegenUtils.class.getClassLoader()); return classLoader.loadClass(name); } finally { IOUtilFunctions.closeSilently(classLoader); } } catch (Exception ex) { LOG.error("Failed to compile class " + name + ": \n" + src); throw new DMLRuntimeException("Failed to compile class " + name + ".", ex); } }
From source file:org.auroraide.server.jci.compilers.Jsr199JavaCompiler.java
public CompilationResult compile(final String[] pResourcePaths, final ResourceReader pReader, final ResourceStore pStore, final ClassLoader classLoader, JavaCompilerSettings settings) { final Collection<JavaFileObject> units = new ArrayList<JavaFileObject>(); for (int i = 0; i < pResourcePaths.length; i++) { final String sourcePath = pResourcePaths[i]; log.debug("compiling " + sourcePath); final String source = pResourcePaths[i]; if (pReader.isAvailable(source)) units.add(new CompilationUnit(sourcePath, pReader)); }//ww w .j a va2 s .c o m final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); final JavaFileManager fileManager = new JciJavaFileManager(units, pStore); final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); // final JavaFileManager fileManager1 = // compiler.getStandardFileManager(null,null,null); System.getProperties(); compiler.getTask(null, fileManager, diagnostics, options, null, units).call(); try { fileManager.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } final List<Diagnostic<? extends JavaFileObject>> jsrProblems = diagnostics.getDiagnostics(); final CompilationProblem[] problems = new CompilationProblem[jsrProblems.size()]; int i = 0; for (final Diagnostic<? extends JavaFileObject> jsrProblem : jsrProblems) { problems[i++] = new Jsr199CompilationProblem(jsrProblem); if (problemHandler != null) { problemHandler.handle(problems[i - 1]); } } return new CompilationResult(problems); }