java - 使用 JavaCompiler 编译目录

标签 java file java-compiler-api

我有以下代码用于使用 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();

我的问题是:如何更改它以编译特定目录中的所有源文件?

最佳答案

从目录中获取所有文件(使用 directory.listFiles() ),并将结果数组传递给 getJavaFileObject(File...)

关于java - 使用 JavaCompiler 编译目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10956404/

相关文章:

java - java打印日期和时间

java - @Override 编译报错,实现一个接口(interface)(eclipse jdk1.6.0_23 linux)

file - 将文件名中具有特定字符串的目录中的所有文件复制到 Bash 中的不同目录

c - 在 C 中将 NULL 字符写入文件

java - 如何在运行时编译 Protocol Buffer 模式?

java - 拼图 : what's wrong with this snippet

java - Mockito - stub 通用类参数

c# - 文件夹事件中的新文件

java - 为什么 Java 在语句末尾不显示双分号错误?

java - 我可以在 Google App Engine 中使用 JavaCompiler 吗?