0

I have the following which code which is being used to compile single files with JavaCompiler:

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(file.getAbsolutePath()));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits);
boolean success = task.call();
fileManager.close();

My question is: How do I change this to compile all source files in a particular directory?

1
  • For better help sooner, post an SSCCE. Commented Jun 9, 2012 at 1:05

1 Answer 1

1

Get all the files from the directory (using directory.listFiles()), and pass the resulting array to getJavaFileObject(File...)

Sign up to request clarification or add additional context in comments.

2 Comments

This gives me: Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: directories not supported at com.sun.tools.javac.util.DefaultFileManager$RegularFileObject.<init>
Make sure to filter out the unwanted files using listFiles(FileFilter) : docs.oracle.com/javase/6/docs/api/java/io/…. You really need to learn reading the API doc. It's intended to be read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.