Nachtfalken
Nachtfalken

Reputation: 69

(JavaCompiler) Compiling multiple files at the same time

So I'm working on building a program that uses the built in JavaCompiler API to compile a directory of .java files. I got it parse through a directory to compile the files, and even compiles them, just not in the order I need. I get back the classic "cannot find symbol" since certain classes are reliant on one another. So a group of files that compiled fine with javac, fail with my program.

I need a way to either compile them in a certain order (my last option is actually parsing through the file for references, but I'd rather not) or compiling at the same time.

Here's my code:

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");
    }
    }

}

EDIT: Wildcards (ie *.java) do not work in JavaCompiler...

ANSWER: From the comments, I tried instead of files[i].getPath() to pass the compiler a String[] containing all paths for all files. Works Great! Thanks!

Upvotes: 4

Views: 1044

Answers (1)

Nachtfalken
Nachtfalken

Reputation: 69

From the comments, I tried instead of files[i].getPath() to pass the compiler a String[] containing all paths for all files. Below is the solution.

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");
    }
    }

}

Upvotes: 2

Related Questions