Example usage for javax.tools JavaCompiler getStandardFileManager

List of usage examples for javax.tools JavaCompiler getStandardFileManager

Introduction

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

Prototype

StandardJavaFileManager getStandardFileManager(DiagnosticListener<? super JavaFileObject> diagnosticListener,
        Locale locale, Charset charset);

Source Link

Document

Returns a new instance of the standard file manager implementation for this tool.

Usage

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 {//  w  w w. ja v  a2 s. c  om
        System.out.println("Compilation failed");
    }
    fileManager.close();
}

From source file:Main.java

public static void main(String[] args) {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager sjfm = compiler.getStandardFileManager(dc, null, null);
    Iterable<? extends JavaFileObject> fileObjects = sjfm.getJavaFileObjects(args);
    compiler.getTask(null, sjfm, dc, null, null, fileObjects).call();
    for (Diagnostic d : dc.getDiagnostics()) {
        System.out.println(d.getMessage(null));
        System.out.printf("Line number = %d\n", d.getLineNumber());
        System.out.printf("File = %s\n", d.getSource());
    }/*from  www .j a  v a 2 s .  c  o  m*/
}

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);//from w  w  w .  j  a  v a  2 s  .  co  m
    boolean success = task.call();
    fileManager.close();
    System.out.println("Success: " + success);
}

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 ww w  .ja 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:CompileFiles2.java

public static void main(String[] args) {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> dc;
    dc = new DiagnosticCollector<JavaFileObject>();

    StandardJavaFileManager sjfm;
    sjfm = compiler.getStandardFileManager(dc, null, null);

    Iterable<? extends JavaFileObject> fileObjects;
    fileObjects = sjfm.getJavaFileObjects(args);

    compiler.getTask(null, sjfm, dc, null, null, fileObjects).call();

    for (Diagnostic d : dc.getDiagnostics()) {
        System.out.println(d.getMessage(null));
        System.out.printf("Line number = %d\n", d.getLineNumber());
        System.out.printf("File = %s\n", d.getSource());
    }//www. ja  v  a 2  s . co m
}

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 w w w . j  av a 2 s .  c o 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: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   www .j a v  a  2  s.  c o m*/
 *
 * @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.asakusafw.compiler.bootstrap.OperatorCompilerDriver.java

/**
 * ??/*from  w w  w .  j a  va 2  s  . c o  m*/
 * @param sourcePath 
 * @param outputPath ?
 * @param encoding 
 * @param operatorClasses ?
 * @throws IOException ???????
 * @throws IllegalArgumentException ?{@code null}????
 */
public static void compile(File sourcePath, File outputPath, Charset encoding, List<Class<?>> operatorClasses)
        throws IOException {
    if (sourcePath == null) {
        throw new IllegalArgumentException("sourcePath must not be null"); //$NON-NLS-1$
    }
    if (outputPath == null) {
        throw new IllegalArgumentException("outputPath must not be null"); //$NON-NLS-1$
    }
    if (encoding == null) {
        throw new IllegalArgumentException("encoding must not be null"); //$NON-NLS-1$
    }
    if (operatorClasses == null) {
        throw new IllegalArgumentException("operatorClasses must not be null"); //$NON-NLS-1$
    }

    // ??JSR-199?????
    List<File> sourceFiles = toSources(sourcePath, operatorClasses);
    LOG.info(MessageFormat.format("Compiling {0}", sourceFiles));
    List<String> arguments = toArguments(sourcePath, outputPath, encoding);
    LOG.debug("Compiler arguments {}", arguments); //$NON-NLS-1$
    if (outputPath.isDirectory() == false && outputPath.mkdirs() == false) {
        throw new IOException(MessageFormat.format("Failed to create {0}", outputPath));
    }
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        throw new IOException("Failed to create a compiler");
    }
    StandardJavaFileManager files = compiler.getStandardFileManager(null, null, encoding);
    try {
        CompilationTask task = compiler.getTask(null, files, null, arguments, Collections.<String>emptyList(),
                files.getJavaFileObjectsFromFiles(sourceFiles));
        if (task.call() == false) {
            LOG.error("Compilation Failed");
        }
    } finally {
        files.close();
    }
    LOG.info("Completed");
}

From source file:de.xirp.ate.ATEManager.java

/**
 * Compiles the given Java {@link java.io.File} to the
 * corresponding byte code class files./*w  ww. j  a v a2 s.  c  om*/
 * 
 * @param javaFile
 *            The file containing the Java code.
 */
public static void compile(File javaFile) {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

    Iterable<? extends JavaFileObject> compilationUnits = fileManager
            .getJavaFileObjectsFromFiles(Collections.singletonList(javaFile));
    compiler.getTask(new PrintWriter(System.out), fileManager, null, null, null, compilationUnits).call();

    try {
        fileManager.close();
    } catch (IOException e) {
        logClass.error("Error: " + e.getMessage() //$NON-NLS-1$
                + Constants.LINE_SEPARATOR, e);
    }
}

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   ww  w  .  j a  v a2 s .  c o  m
        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();
    }
}