0

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!

2
  • Hi, Facing the same issue. Please help if you got the solution. Commented Jun 11, 2023 at 8:11
  • Hi Hari, the only solution that I found was the one in the accepted answer. If it is not related to the working directory of the Tomcat Server in your case as well, then I unfortunately don't know how to solve it either, without further information Commented Jun 14, 2023 at 16:22

1 Answer 1

1

Apparently it had to do something with the working directory and the folder Tomcat was launched in. Configuring the Server Path under Server Location did the trick for me (Set it to the project root in my workspace).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.