ClassCastException when antlr.jar is in jre/lib/ext
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Charles_Honton
Posted On:   Thursday, April 10, 2003 12:49 PM

I am using antlr-2.7.2 and have placed antlr.jar in the jre's lib extension directory. When running transmogrify, I get the following output: Can't find/access AST Node typenet.sourceforge.transmogrify.symtab.parser.SymTabAST java.lang.ClassCastException: antlr.CommonAST at net.sourceforge.transmogrify.symtab.parser.FileParser.setupTree(FileParser.java:66) at net.sourceforge.transmogrify.symtab.parser.FileParser. (FileParser.java:44) at net.sourceforge.transmogrify.symtab.Main.main(Main.java:56) The problem is that antlr.ASTFactory.setASTNodeClass uses Class.forName(). This method asks for the desired class from the caller's classloader. When antlr is in the exten   More>>

I am using antlr-2.7.2 and have placed antlr.jar in the jre's lib
extension directory. When running transmogrify, I get the following
output:


			Can't find/access AST Node typenet.sourceforge.transmogrify.symtab.parser.SymTabAST
			
java.lang.ClassCastException: antlr.CommonAST
at net.sourceforge.transmogrify.symtab.parser.FileParser.setupTree(FileParser.java:66)
at net.sourceforge.transmogrify.symtab.parser.FileParser. (FileParser.java:44)
at net.sourceforge.transmogrify.symtab.Main.main(Main.java:56)

The problem is that antlr.ASTFactory.setASTNodeClass uses
Class.forName(). This method asks for the desired class from the
caller's classloader. When antlr is in the extension directory, its
classloader is the extension classloader, which knows nothing about
classes on the CLASSPATH.


There are two solutions I can think of to allow antlr.jar to work as
an extension jar. One is to explicitly use the system classloader:


			public void setASTNodeClass(String t) {
			
theASTNodeType = t;
try {
theASTNodeTypeClass = ClassLoader.getSystemClassLoader().loadClass(t); // get class def
}
catch (Exception e) {
// either class not found,
// class is interface/abstract, or
// class or initializer is not accessible.
error("Can't find/access AST Node type " + t);
}
}

Another solution is to pass the class as a parameter:


			public void setASTNodeClass(Class c) {
			
theASTNodeTypeClass = c;
theASTNodeType = c.getName();
}

I'd recommend that both of these patches be added.


chas    <<Less

Re: ClassCastException when antlr.jar is in jre/lib/ext

Posted By:   Terence_Parr  
Posted On:   Saturday, April 12, 2003 03:40 PM

Hi Chas,


weird stuff. Ok, I'll add to things to examine.


Terence

About | Sitemap | Contact