I have an existing "Example Webapp" that references "Example Library" using Maven. I'm running Tomcat 7 inside Eclipse 4.3RC3 with the m2e plugin. When I launch Example Webapp on Tomcat inside Eclipse, I have verified that the example-library.jar
is probably getting deployed in the Tomcat instance's WEB-INF/lib
folder.
The Example Webapp has code that compiles certain classes on the fly using JavaCompiler.CompilationTask
. These dynamically generated classes reference classes in example-library.jar
. Unfortunately the compile task is failing because the referenced classes cannot be found.
I understand that I can set the JavaCompiler
classpath, but System.getProperty("java.class.path")
only returns me the Tomcat classpath, not the webapp classpath:
C:\bin\tomcat\bin\bootstrap.jar;C:\bin\tomcat\bin\tomcat-juli.jar;C:\bin\jdk6\lib\tools.jar
Other have said that I need to get the real path of WEB-INF/lib
from the servlet context, but the class generation code doesn't know anything about a servlet context --- it is written to be agnostic of whether it is used on the client or on the server.
In another question, one answer indicated I could enumerate the classloader URLs, and sure enough this provides me with the jars in WEB-INF/lib
, but when I provide this as a -classpath
option to compiler.getTask()
, the task still fails because it can't find the referenced classes.
How can I simply provide the classpath of the currently executing code to the JavaCompiler
instance so that it will find the classes from the libraries in WEB-INF/lib
? (A similar question was raised but never answered regarding referencing jars within ear files using JavaCompiler
.)
Example: In an attempt to get things working at any cost, I even tried to hard-code the classpath. For example, I have foobar.lib
in my webapp lib directory, so I used the following code, modified from the answers I indicated above:
List<String> options = new ArrayList<String>();
options.add("-classpath");
options.add("C:\\work\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\FooBar\\WEB-INF\\lib\\foobar.jar");
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
boolean success = task.call();
In the end success
is false
, and my diaognostics
indicates package com.example.foo.bar does not exist...
, even though that package is in foobar.jar
.