简体   繁体   English

从文件获取类

[英]Get Classes from File

I have a sourcecode and in this source are two classes referenced. 我有一个源代码,在此source中引用了两个classes

Now I want to compile this code via JavaCompiler but therefore I have to first compile the two referenced classes . 现在,我想通过JavaCompiler compile此代码,但是因此我必须首先编译两个引用的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 XTest.java和YTest.java与Suite在同一个目录中

Now I want to compile this code via JavaCompiler but therefore I have to first compile the two referenced classes 现在,我想通过JavaCompiler编译此代码,但是因此我必须首先编译两个引用的类

No you don't. 不,你不会。 As long as the Java compiler can find those classes it will compile them automatically. 只要Java编译器可以找到这些类,它将自动编译它们。 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. 这意味着它们需要与您实际编译的文件位于同一目录系统中,或者可以通过-sourcepath选项定位。

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. 对于JavaCompiler建议,我认为加入他们的迭代集合compilationUnits提供给getTask(..)应该达到预期的效果。

Yes, there is definitiveliy a better method. 是的,肯定有更好的方法。 Use a build tool like Apache Ant to organize your builds. 使用Apache Ant之类的构建工具来组织构建。 There are several tutorials included in the Ant Wiki. Ant Wiki中包含一些教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM