Tomcat webapp ClassLoader does not work with runtime compilation

I have some Java classes that are generated at runtime and compiled with

JavaCompiler.CompilationTask .

The generated code refers to classes that are in other projects that are maven dependencies of my project. Everything works fine locally, but when I deploy the Dev server to the Dev server, I get a bunch

"package xxx does not exist"

and

"cannot find symbol"

errors in the compilation task. I checked the WEB-INF / lib directory in the Webapp project deployed by Tomcat and all the banks there, including project dependencies. Shouldn't everything in the WEB-INF/lib directory be accessible to the Tomcat project at run time?

EDIT: Here is my context.xml:

 <?xml version="1.0" encoding="UTF-8"?> <Context> <Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="${catalina.home}/webapps/kdweb/WEB-INF/lib/acommons-0.0.1-SNAPSHOT.jar;${catalina.home}/webapps/kdweb/WEB-INF/lib/abizcommons-0.0.1-SNAPSHOT.jar;${catalina.home}/webapps/kdweb/WEB-INF/lib/kd_market_data-common-0.0.1-SNAPSHOT.jar;${catalina.home}/webapps/kdweb/WEB-INF/lib/aggregation-0.0.1-SNAPSHOT.jar;${catalina.home}/webapps/kdweb/WEB-INF/lib/apods-client-0.0.1-SNAPSHOT.jar;${catalina.home}/webapps/kdweb/WEB-INF/lib/framework-common-0.0.1-SNAPSHOT.jar" /> </Context> 
+4
source share
2 answers

you can use Loader and set reloadable to true. ( This may also be related)

+1
source

Turns out I needed to pass an explicit path to JavaCompiler.CompilationTask in code. Here's how I got it to work:

 String classPath = "webapps/WEB-INF/lib/jar1.jar;webapps/WEB-INF/lib/jar2.jar"; List<String> options = new ArrayList<String>(); options.addAll(Arrays.asList("-classpath", classPath)); final JavaCompiler.CompilationTask task = compiler.getTask(null, manager, null, options, null, Arrays.asList(source)); 
+1
source

Source: https://habr.com/ru/post/1435048/


All Articles