Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top