(JavaCompiler) 同时编译多个文件

(JavaCompiler) Compiling multiple files at the same time

所以我正在构建一个程序,该程序使用内置的 JavaCompiler API 来编译 .java 文件的目录。我通过一个目录解析它来编译文件,甚至编译它们,只是不是按照我需要的顺序。我找回了经典的 "cannot find symbol",因为某些 类 相互依赖。因此,一组使用 javac 编译良好的文件在我的程序中失败了。

我需要一种方法来按特定顺序编译它们(我的最后一个选项实际上是解析文件以获取引用,但我宁愿不这样做)或同时编译。

这是我的代码:

import javax.tools.*;
import java.io.*;
import java.util.*;

public class SimpleCompileTest 
{
    public static void main(String[] args) 
    {

        try{
            File[] files;
            File dir = new File("Thing");
            files = dir.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.toLowerCase().endsWith(".java");
                }
            });

        File file = new File("Errors.txt");
        try{
            FileOutputStream errorStream = new FileOutputStream("Errors.txt");

            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            for(int i = 0; i < files.length; i++)
            {
                int compilationResult = compiler.run(null, null, errorStream, files[i].getPath());
                        if(compilationResult == 0){
                            System.out.println("Compilation is successful");
                        }else{
                            System.out.println("Compilation Failed");
                        }
            }
        }catch(Exception e)
        {
            System.out.println("error in compiler");
        }
    }catch(Exception h)
    {
        System.out.println("error in filename");
    }
    }

}

编辑:通配符(即 *.java)在 JavaCompiler 中不起作用...

答案:根据评论,我尝试代替 files[i].getPath() 向编译器传递一个包含所有文件的所有路径的 String[]。很好用!谢谢!

根据评论,我尝试代替 files[i].getPath() 向编译器传递一个包含所有文件的所有路径的 String[]。下面是解决方法。

import javax.tools.*;
import java.io.*;
import java.util.*;

public class SimpleCompileTest 
{
    public static void main(String[] args) 
    {

        try{
            File[] files;
            File dir = new File("Thing");
            files = dir.listFiles(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.toLowerCase().endsWith(".java");
                }
            });
            String[] filenames = new String[files.length];
            for(int i = 0; i < files.length; i++)
                filenames[i] = files[i].getName();

        File file = new File("Errors.txt");
        try{
            FileOutputStream errorStream = new FileOutputStream("Errors.txt");

            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

                int compilationResult = compiler.run(null, null, errorStream, filenames);
                        if(compilationResult == 0){
                            System.out.println("Compilation is successful");
                        }else{
                            System.out.println("Compilation Failed");
                        }
        }catch(Exception e)
        {
            System.out.println("error in compiler");
        }
    }catch(Exception h)
    {
        System.out.println("error in filename");
    }
    }

}