I have a “Webapp Example” that references the “Example Library” using Maven. I am running Tomcat 7 inside Eclipse 4.3RC3 with the m2e plugin. When I run the Webapp example on Tomcat inside Eclipse, I confirmed that example-library.jar is likely to be deployed to the Tomcat instance folder WEB-INF/lib .
Example Webapp has code that compiles certain classes on the fly using JavaCompiler.CompilationTask . These dynamically generated classes map classes in example-library.jar . Unfortunately, the compilation task fails because the reference classes cannot be found.
I understand that I can set the JavaCompiler classpath , but System.getProperty("java.class.path") returns me the path of the Tomcat class, not the path of the webapp path:
C:\bin\tomcat\bin\bootstrap.jar;C:\bin\tomcat\bin\tomcat-juli.jar;C:\bin\jdk6\lib\tools.jar
Others said that I need to get the real WEB-INF/lib path from the servlet context, but the class generation code knows nothing about the context servlet --- it is written as an agnostic about whether it is used on the client or on the server.
In another question , one answer indicated that I can list the URLs of the class loader, and, of course, I provide this with banks in WEB-INF/lib , but when I provide this as the -classpath option for compiler.getTask() , the task still fails because it cannot find the reference classes.
How can I just provide the class path of the current executable code to the JavaCompiler instance so that it finds classes from libraries in WEB-INF/lib ? (A similar question was raised, but he never answered regarding jar links in ear files using JavaCompiler .)
Example. In an attempt to get things to work at all costs, I even tried hardcoded the classpath. For example, I have foobar.lib in my webapp lib directory, so I used the following code, modified from the answers 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();
At the end, success is false , and my diaognostics indicates package com.example.foo.bar does not exist... although this package is in foobar.jar .