Note that there are some explanatory texts on larger screens.

plurals
  1. POUse webapp classpath using JavaCompiler in Tomcat within Eclipse with Maven
    primarykey
    data
    text
    <p>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 <code>example-library.jar</code> is probably getting deployed in the Tomcat instance's <code>WEB-INF/lib</code> folder.</p> <p>The Example Webapp has code that compiles certain classes on the fly using <code>JavaCompiler.CompilationTask</code>. These dynamically generated classes reference classes in <code>example-library.jar</code>. Unfortunately the compile task is failing because the referenced classes cannot be found.</p> <p>I understand that I can <a href="https://stackoverflow.com/questions/1563909">set the <code>JavaCompiler</code> classpath</a>, but <code>System.getProperty("java.class.path")</code> only returns me the Tomcat classpath, not the webapp classpath:</p> <pre><code>C:\bin\tomcat\bin\bootstrap.jar;C:\bin\tomcat\bin\tomcat-juli.jar;C:\bin\jdk6\lib\tools.jar </code></pre> <p>Other have <a href="https://stackoverflow.com/questions/4273039">said</a> that I need to get the real path of <code>WEB-INF/lib</code> 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.</p> <p>In <a href="https://stackoverflow.com/questions/274474">another question</a>, one <a href="https://stackoverflow.com/a/665896/421049">answer</a> indicated I could enumerate the classloader URLs, and sure enough this provides me with the jars in <code>WEB-INF/lib</code>, but when I provide this as a <code>-classpath</code> option to <code>compiler.getTask()</code>, the task still fails because it can't find the referenced classes.</p> <p>How can I simply provide the classpath of the currently executing code to the <code>JavaCompiler</code> instance so that it will find the classes from the libraries in <code>WEB-INF/lib</code>? (A <a href="https://stackoverflow.com/questions/16477606">similar question</a> was raised but never answered regarding referencing jars within ear files using <code>JavaCompiler</code>.)</p> <p><em>Example:</em> In an attempt to get things working at any cost, I even tried to hard-code the classpath. For example, I have <code>foobar.lib</code> in my webapp lib directory, so I used the following code, modified from the answers I indicated above:</p> <pre><code>List&lt;String&gt; options = new ArrayList&lt;String&gt;(); 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(); </code></pre> <p>In the end <code>success</code> is <code>false</code>, and my <code>diaognostics</code> indicates <code>package com.example.foo.bar does not exist...</code>, even though that package is in <code>foobar.jar</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload