I have a sourcecode and in this source are two classes referenced.

Now I want to compile this code via JavaCompiler but therefore I have to first compile the two referenced classes.

Is there a better method than reading the file and search for the classes, to compile them first?

package data.testcases

import org.junit.runner.RunWith;
import org.junit.runners.Suite;


@RunWith(Suite.class)
@Suite.SuiteClasses({
        XTest.class,
        YTest.class
})
public class ASuite {
}

XTest.java and YTest.java are in the same dir as Suite is

有帮助吗?

解决方案

Now I want to compile this code via JavaCompiler but therefore I have to first compile the two referenced classes

No you don't. As long as the Java compiler can find those classes it will compile them automatically. That means that they need to be located in the same directory system as the file you are actually compiling, or else locatable via the -sourcepath option.

其他提示

Looking at the docs. for JavaCompiler suggests to me that adding them to the iterable collection of compilationUnits provided to getTask(..) should achieve the desired result.

Yes, there is definitiveliy a better method. Use a build tool like Apache Ant to organize your builds. There are several tutorials included in the Ant Wiki.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top