Agreed.
> So, given a ClassLoader is there an easy way
> to reconstruct the classpath?
I'm pretty sure the answer to this is "no". A ClassLoader doesn't even
need to have a concept of a "classpath". To a large extendt the public
interface of a ClassLoader is just a map from (fully-qualified) class
names to Class objects.
> As it turns out for Tomcat there is a
> WebappClassLoader that provides getters to get at all the required jars.
> But, that's a very specific solution and I'd rather come up with something
> more general. Or, is there an some way to tell JavaCompiler to use a
> classpath instead of having to specify the classpath as a String argument?
Do you mean "use a ClassLoader instead"...? If so, that's what I was
hoping the JavaFileManagerImpl.getClassLoader would let you do. Did
you have any luck debugging the open jdk JavacTool to see why it was
failing despite giving it the right classloader?
> Second, the defineClass(byte[] classFile) method had to change to install
> the new class in the right ClassLoader.
>
> private static Class defineClass(byte[] classFile)
> throws Throwable {
> Object[] args = new Object[]{ null, classFile,
> new Integer(0), new Integer(classFile.length),
> PROTECTION_DOMAIN };
> try {
> // Used to be ClassLoader.getSystemClassLoader(), webapps are unable to
> find the newly created class if it is defined here. Instead, just use the
> ClassLoader that loaded this class.
> return (Class)
> DEFINE_CLASS.invoke(StubGxpTemplate.class.getClassLoader(), args);
> } catch (InvocationTargetException e) {
> throw e.getCause();
> }
> }
Oh, of course. :-Q
Technically, instead of StubGxpTemplate's ClassLoader, that should
probably be the ClassLoader that loaded the stub template, though I
can't think of a situation where they'd actually be different. I'm
guessing that before you made this change you got a very different
error from the javac error that started this thread. Is that right?
Yes JSPs can reference other classes, but it has to do so using tag
libraries, so I make a list of the tag libraries used and find the
jars they were loaded from and add it to the classpath. This does not
include scriptlets, but I didn't care about those.
> I keep wondering why the JavaFileManager.getClassLoader() approach
> isn't working. Just based on the docs it seems like it's the "right"
> way to do what we're trying to do, but it sounds like theory and
> practice may not be in agreement.
I also tried that way but I had a hard time understanding it, and the
documentation is very little.
musachy