List of usage examples for javax.tools ToolProvider getSystemJavaCompiler
public static JavaCompiler getSystemJavaCompiler()
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.//w ww . j av a 2 s .com * * @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.phoenix.end2end.UserDefinedFunctionsIT.java
/** * Compiles the test class with bogus code into a .class file. * Upon finish, the bogus jar will be left at dynamic.jar.dir location *//*from w w w . j a v a 2s . c o m*/ private static void compileTestClass(String className, String program, int counter) throws Exception { String javaFileName = className + ".java"; File javaFile = new File(javaFileName); String classFileName = className + ".class"; File classFile = new File(classFileName); String jarName = "myjar" + counter + ".jar"; String jarPath = "." + File.separator + jarName; File jarFile = new File(jarPath); try { String packageName = "org.apache.phoenix.end2end"; FileOutputStream fos = new FileOutputStream(javaFileName); fos.write(program.getBytes()); fos.close(); JavaCompiler jc = ToolProvider.getSystemJavaCompiler(); int result = jc.run(null, null, null, javaFileName); assertEquals(0, result); Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); FileOutputStream jarFos = new FileOutputStream(jarPath); JarOutputStream jarOutputStream = new JarOutputStream(jarFos, manifest); String pathToAdd = packageName.replace('.', '/') + '/'; String jarPathStr = new String(pathToAdd); Set<String> pathsInJar = new HashSet<String>(); while (pathsInJar.add(jarPathStr)) { int ix = jarPathStr.lastIndexOf('/', jarPathStr.length() - 2); if (ix < 0) { break; } jarPathStr = jarPathStr.substring(0, ix); } for (String pathInJar : pathsInJar) { jarOutputStream.putNextEntry(new JarEntry(pathInJar)); jarOutputStream.closeEntry(); } jarOutputStream.putNextEntry(new JarEntry(pathToAdd + classFile.getName())); byte[] allBytes = new byte[(int) classFile.length()]; FileInputStream fis = new FileInputStream(classFile); fis.read(allBytes); fis.close(); jarOutputStream.write(allBytes); jarOutputStream.closeEntry(); jarOutputStream.close(); jarFos.close(); assertTrue(jarFile.exists()); Connection conn = driver.connect(url, EMPTY_PROPS); Statement stmt = conn.createStatement(); stmt.execute("add jars '" + jarFile.getAbsolutePath() + "'"); } finally { if (javaFile != null) javaFile.delete(); if (classFile != null) classFile.delete(); if (jarFile != null) jarFile.delete(); } }
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/* www . j a va 2 s . co 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 ww. j a va2 s . co m 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> compileTestConnectorAndDependency() throws Exception { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if (compiler == null) { throw new IllegalStateException( "Cannot find the system Java compiler. " + "Check that your class path includes tools.jar"); }//w w w .ja v a2s . c o m Path outputDir = Files.createTempDirectory(null); Map<String, JarContents> sourceFileToJarMap = new HashMap<>(); ClassLoader classLoader = getClass().getClassLoader(); List<File> sourceFiles = new ArrayList<>(); File file = new File(classLoader.getResource("TestConnector/TestConnector.java").getFile()); sourceFiles.add(file); file = new File(classLoader.getResource("TestConnector/TestLinkConfiguration.java").getFile()); sourceFiles.add(file); file = new File(classLoader.getResource("TestConnector/TestLoader.java").getFile()); sourceFiles.add(file); file = new File(classLoader.getResource("TestConnector/TestToDestroyer.java").getFile()); sourceFiles.add(file); file = new File(classLoader.getResource("TestConnector/TestToInitializer.java").getFile()); sourceFiles.add(file); file = new File(classLoader.getResource("TestConnector/TestToJobConfiguration.java").getFile()); sourceFiles.add(file); List<File> propertiesFiles = new ArrayList<>(); file = new File(classLoader.getResource("TestConnector/sqoopconnector.properties").getFile()); propertiesFiles.add(file); sourceFileToJarMap.put("test-connector.jar", new JarContents(sourceFiles, propertiesFiles)); sourceFiles = new ArrayList<>(); file = new File(classLoader.getResource("TestConnector/TestDependency.java").getFile()); sourceFiles.add(file); sourceFileToJarMap.put("test-dependency.jar", new JarContents(sourceFiles, ListUtils.EMPTY_LIST)); return buildJar(outputDir.toString(), sourceFileToJarMap); }
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); }// ww w.ja 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 a2 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); } } 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 *///from w w w . j a v a2 s . c o m 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 {//from w ww . java 2s. c o m //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)); }/*w w w .jav a2s . 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); }