I am working on a web project where I create a custom .java-File, load it with a custom ClassLoader, compile it using the Compiler API and then execute it via Reflection. The only thing that doesn't seem to work is compiling the file which looks like that:
package testfiles;
import exceptions.IncorrectSolutionException;
public class TestFile {
public static void testSolution() throws IncorrectSolutionException {
//some code here
}
}
Using the Diagnostic-Collector of the Compiler-API, I get the following error-message:
package exceptions does not exist, line 2 cannot find symbol
symbol: class IncorrectSolutionException
The package 'exceptions' and the referred class definitely exist and my src-folder is structured like that: (folder with class files as well)
- src
- exceptions
- testfiles
I tried setting the classpath as suggested here but it doesn't change anything.
How I compile the file:
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
}
Do I have to add another option to the CompilationTask or is the problem somewhere else? Thanks in advance!