4

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!

  • did you try with javac *.java? – Razib Mar 05 '15 at 21:05
  • 6
    It looks like you ought to be passing all the files to `compiler.run` in one go, not just one at a time? Instead of `files[i].getPath()`, pass it a `String[]` containing all paths for all files. – Louis Wasserman Mar 05 '15 at 21:06
  • Mr. Wasserman, you are a life saver, I didn't realize the solution was that simple. I got the files[].getName into a string array and sent it in instead and I'm compiling groups of javas now perfectly. Thank you a bunch, sir! – Nachtfalken Mar 05 '15 at 22:11
  • 1
    If you have answered your question, please feel encouraged to add it as an answer instead. It increases the visibility of the solution. – Makoto Mar 05 '15 at 22:16
  • I will do that then! – Nachtfalken Mar 05 '15 at 22:21

1 Answers1

2

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

}