[antlr-interest] ANTLR and Class.forName()

Max Rydahl Andersen max.andersen at jboss.com
Mon Mar 14 01:44:15 PST 2005


Hi,

In Hibernate3 we started using antlr for our HQL parser and it has worked
out great! Made the parser much more extensible.

Still, we've found a major problem in ANTLR with respect to classloading
when running inside e.g. a J2EE container.

The antlr code has about 11 instances of dynamic classloading where
it naively uses Class.forName to load classes. That is "bad" behavior
for a framework that is meant to be portable :(

The solution is to use something like:

public static Class classForName(String name) throws ClassNotFoundException 
{

try {

return Thread.currentThread().getContextClassLoader().loadClass(name);

}

catch (Exception e) {

return Class.forName(name);

}

}

which uses the current context classloader BEFORE trying to use 
Class.forName().

By not using the context classloader you can get into all kind of problems 
in containers

that might be having antlr in their classpath for whatever reason and then 
antlr cant load the classes

which are available in ones j2ee app.

There is the workaround of putting all your jars in the system boot or 
startup path, but that is

a real bad workaround since it affects all jars that is dependent on it.

I can provide a patch for Antlr against the latest release if you want me 
to - should this

just go to this mailinglist of is there issue tracker available somewhere ?



/max





More information about the antlr-interest mailing list