Java 编译器 API:自定义包的导入不起作用

Java Compiler API: import of custom package not working

我正在开发一个 Web 项目,我在其中创建了一个自定义 .java-File,使用自定义 ClassLoader 加载它,使用编译器 API 编译它,然后通过反射执行它。唯一似乎不起作用的是编译看起来像这样的文件:

package testfiles; 
import exceptions.IncorrectSolutionException; 
public class TestFile { 
  public static void testSolution() throws IncorrectSolutionException {
    //some code here
  }
}

使用编译器的诊断收集器-API,我收到以下错误消息:

package exceptions does not exist, line 2 cannot find symbol
symbol:   class IncorrectSolutionException

包 'exceptions' 和引用的 class 肯定存在,我的 src 文件夹的结构如下:(文件夹也有 class 个文件)

我尝试按照建议设置 class 路径 here 但它没有任何改变。

我如何编译文件:

DiagnosticCollector< JavaFileObject > diagnostics = new DiagnosticCollector<>();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null, files);
File file = new File(absolute path);
//I am using the absolute path here, because I had problems resolving the relative path as my working directory seems to be the folder eclipse is installed in.
Iterable<? extends JavaFileObject> files = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(file));
if(task.call()) {
    System.out.println("compiling File");
} else {
    //...handle Diagnostics
}

我是否必须向 CompilationTask 添加另一个选项或者是其他地方的问题? 提前致谢!

显然它必须对工作目录做一些事情,文件夹 Tomcat 在其中启动。在服务器位置下配置服务器路径对我有用(将其设置为我工作区中的项目根目录) .