views:

190

answers:

1

I'm trying to compile many files using the Compiler API.

Say I have a directory structure

.../program
   +/org
    +/foo
    |+ Main.java
    +/bar
     + Tools.java

Is there any way to discover all the Java files and make it compiler everything without resorting to recursing into all the directories and finding all the *.java files?

EDIT: What I'm trying to do is compile all the classes that I get in some directory tree. These classes are independent of each other. Then I load the classes and instantiate some objects of these classes and call methods. None of the classes need to have main.

+2  A: 

The javax.tools.JavaFileManager class has a list() method that should do the trick:

Iterable<JavaFileObject> list(JavaFileManager.Location location,
                              String packageName,
                              Set<JavaFileObject.Kind> kinds,
                              boolean recurse)
                              throws IOException

Use the recurse parameter to include "subpackages" and then just iterate over the returned JavaFileObject.

Pascal Thivent