Example usage for javax.tools StandardJavaFileManager getJavaFileObjectsFromFiles

List of usage examples for javax.tools StandardJavaFileManager getJavaFileObjectsFromFiles

Introduction

In this page you can find the example usage for javax.tools StandardJavaFileManager getJavaFileObjectsFromFiles.

Prototype

Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files);

Source Link

Document

Returns file objects representing the given files.

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 ww.j  a v a 2s. com*/
        System.out.println("Compilation failed");
    }
    fileManager.close();
}

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   w  w w.  j  a v a 2s  . co  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:DiagnosticDemo.java

public static void main(String[] args) {
    String sourceFile = "c:/HelloWorld.Java";
    JavaCompilerTool compiler = ToolProvider.getSystemJavaCompilerTool();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager =

            compiler.getStandardFileManager(diagnostics);

    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.run();/*from w  w w.ja v  a 2s.  c o  m*/
    try {
        fileManager.close();
    } catch (IOException e) {
    }
    List<Diagnostic<? extends JavaFileObject>> diagnosticList = diagnostics.getDiagnostics();
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticList) {
        System.out.println("Position:" + diagnostic.getStartPosition());
    }
}

From source file:JavaCompilerDemo.java

public static void main(String[] args) {
    String sourceFile = "c:/HelloWorld.Java";
    JavaCompilerTool compiler = ToolProvider.getSystemJavaCompilerTool();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null);

    // prepare the source file(s) to compile
    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.run();//from  www .  ja  va 2  s .co  m
    boolean result = task.getResult();
    if (result) {
        System.out.println("Compilation was successful");
    } else {
        System.out.println("Compilation failed");
    }
    try {
        fileManager.close();
    } catch (IOException e) {
    }
}

From source file:clear.cdb.support.test.AnnotationProcessorTestCompiler.java

private static Iterable<? extends JavaFileObject> getCompilationUnitOfClass(StandardJavaFileManager fileManager,
        String classToCompile) throws IOException {
    ClassPathResource resource = new ClassPathResource(classToCompile + ".java");
    return fileManager.getJavaFileObjectsFromFiles(Collections.singletonList(resource.getFile()));
}

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./* w  w  w  .  jav a 2 s .co  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 ww  w. j  av a 2  s . com
 * @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.//ww w  .j  a v a2  s.  c o  m
 * 
 * @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:adalid.util.meta.MetaJavaCompiler.java

public static void compile() {
    String dp1 = System.getProperties().getProperty("user.dir");
    String sep = System.getProperties().getProperty("file.separator");
    File src = new File(dp1 + sep + "src");
    File trg = new File(dp1 + sep + "bin");
    boolean dir = trg.isDirectory() || trg.mkdirs();
    List<File> files = getJavaFiles(src);
    // Get the java compiler provided with this platform
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    // Get the writer for the compiler output
    StringWriter out = new StringWriter();
    // Get the diagnostic listener for non-fatal diagnostics; if null use the compiler's default method for reporting diagnostics
    DiagnosticListener<JavaFileObject> diagnosticListener1 = null; // new DiagnosticCollector<JavaFileObject>();
    // Get the locale to apply when formatting diagnostics; null means the default locale
    Locale locale = null;//from  ww  w. ja  va  2  s.  c o  m
    // Get the character set used for decoding bytes; if null use the platform default
    Charset charset = null;
    // Get an instance of the standard file manager implementation
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticListener1, locale, charset);
    // Get the diagnostic listener for non-fatal diagnostics; if null use the compiler's default method for reporting diagnostics
    DiagnosticListener<JavaFileObject> diagnosticListener2 = null; // new DiagnosticCollector<JavaFileObject>();
    // Get the list of compiler options, null means no options
    List<String> options = Arrays
            .asList(new String[] { "-g", "-source", "1.7", "-Xlint:unchecked", "-d", trg.getAbsolutePath() });
    // Get the list of class names (for annotation processing), null means no class names
    List<String> classes = null;
    // Get the list of java file objects to compile
    Iterable<? extends JavaFileObject> units = fileManager.getJavaFileObjectsFromFiles(files);
    // Create the compilation task
    CompilationTask task = compiler.getTask(out, fileManager, diagnosticListener2, options, classes, units);
    // Perform the compilation task
    // task.call(); // java.lang.NullPointerException at com.sun.tools.javac.api.JavacTaskImpl.parse(JavacTaskImpl.java:228)
    if (task instanceof JavacTask) {
        javacTask(task);
    }
}

From source file:com.googlecode.jsonschema2pojo.integration.util.Compiler.java

public void compile(File directory, String classpath) {

    JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null, null, null);

    Iterable<? extends JavaFileObject> compilationUnits = fileManager
            .getJavaFileObjectsFromFiles(findAllSourceFiles(directory));

    if (compilationUnits.iterator().hasNext()) {
        Boolean success = javaCompiler
                .getTask(null, fileManager, null, asList("-classpath", classpath), null, compilationUnits)
                .call();//from  w w w.j  a v  a2s. co  m
        assertThat("Compilation was not successful, check stdout for errors", success, is(true));
    }

}