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