Example usage for javax.tools JavaCompiler run

List of usage examples for javax.tools JavaCompiler run

Introduction

In this page you can find the example usage for javax.tools JavaCompiler run.

Prototype

int run(InputStream in, OutputStream out, OutputStream err, String... arguments);

Source Link

Document

Run the tool with the given I/O channels and arguments.

Usage

From source file:gov.anl.cue.arcane.engine.matrix.MatrixModel.java

/**
 * Formulate bytecode.//from www .  j  ava 2 s. c  o  m
 */
public void formulateBytecode() {

    // Attempt to compile the file.
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    compiler.run(null, null, null,
            Util.TEMP_DIR + MatrixModel.PACKAGE_PATH + MatrixModel.CONCRETE_CLASS_NAME + ".java");

}

From source file:org.apache.hadoop.hbase.TestClassFinder.java

/**
 * Compiles the test class with bogus code into a .class file.
 * Unfortunately it's very tedious.//from w  ww . ja v a  2s. c o  m
 * @param counter Unique test counter.
 * @param packageNameSuffix Package name suffix (e.g. ".suffix") for nesting, or "".
 * @return The resulting .class file and the location in jar it is supposed to go to.
 */
private static FileAndPath compileTestClass(long counter, String packageNameSuffix, String classNamePrefix)
        throws Exception {
    classNamePrefix = classNamePrefix + counter;
    String packageName = makePackageName(packageNameSuffix, counter);
    String javaPath = basePath + classNamePrefix + ".java";
    String classPath = basePath + classNamePrefix + ".class";
    PrintStream source = new PrintStream(javaPath);
    source.println("package " + packageName + ";");
    source.println("public class " + classNamePrefix + " { public static void main(String[] args) { } };");
    source.close();
    JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
    int result = jc.run(null, null, null, javaPath);
    assertEquals(0, result);
    File classFile = new File(classPath);
    assertTrue(classFile.exists());
    return new FileAndPath(packageName.replace('.', '/') + '/', classFile);
}

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  ww  w  .j ava 2 s .  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.ebayopensource.turmeric.eclipse.test.util.ProjectArtifactValidator.java

private void compileSrc(String filePath) {

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

    int result = compiler.run(null, null, null, filePath);
}

From source file:org.ebayopensource.turmeric.tools.codegen.AbstractServiceGeneratorTestCase.java

protected void compileJavaFile(String file) {
    JavaCompiler compiler = (JavaCompiler) ToolProvider.getSystemJavaCompiler();
    compiler.run(null, null, null, file);
}

From source file:org.spiffyui.maven.plugins.JavaCompileMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    ArgBuilder args = new ArgBuilder();

    String classpath = flatten(classpathElements.toString(), File.pathSeparator);

    if (debug) {//from   ww  w .  jav a  2s .  com
        args.add("-g");
    }

    if (verbose) {
        args.add("-verbose");
    }

    if (deprecation) {
        args.add("-deprecation");
    }

    if (nowarn) {
        args.add("-nowarn");
    }

    if (!xlint.equals("none")) {
        if (xlint.equals("all")) {
            args.add("-Xlint");
        } else {
            args.add("-Xlint:" + xlint);
        }
    }

    args.add("-d").add(outputDirectory.getAbsolutePath()).add("-s")
            .add(generatedSourcesDirectory.getAbsolutePath()).add("-encoding").add(encoding).add("-cp")
            .add(classpath).add("-source").add(source).add("-target").add(target);

    String[] exts = { "java" };

    for (String extraArg : extraArgs) {
        args.add(extraArg);
    }

    List<String> sources = compileSourceRoots;
    sources.add(generatedSourcePath);

    for (String s : sources) {
        File path = new File(s);

        if (!path.exists()) {
            continue;
        }

        List<File> files = new ArrayList<File>(FileUtils.listFiles(path, exts, true));
        for (File file : files) {
            args.add(file.getAbsolutePath());
        }
    }

    ensureExists(outputDirectory);
    ensureExists(generatedSourcesDirectory);

    JavaCompiler javac = ToolProvider.getSystemJavaCompiler();

    if (javac == null) {
        throw new MojoExecutionException("The Java compiler could not be found.  "
                + "Make sure to point your Maven build to a full Java development environment.");
    }

    getLog().debug("Exec: javac " + args.toString());
    if (javac.run(null, null, null, args.get()) != 0) {
        throw new MojoExecutionException("Compilation failed");
    }
}