OT: This is for java geeks out there

Andrew Haley aph at redhat.com
Fri Sep 5 09:22:25 UTC 2008


Christoph Höger wrote:
> Hi,
> 
> I do not know where to ask that, so I try it here:
> 
> I want to load a (dynamically created) class via URL ClassLoader and
> then invoke a public static method on it.
> 
> Here is the code:
> 
> 		Class lexerDefs = Class.forName("parser.LexerDefs", true, loader);
> 		
> 		Method getLexerDefs = null;
> 		
> 		try {
> 			getLexerDefs = lexerDefs.getMethod("lexerDefs", new Class[] {});
> 		} catch (NoSuchMethodException e) {
> 			fail("Class " + lexerDefs.getName() + " has no method lexerDefs()\n"
> + e.getMessage());
> 		}
> 		
> 		parser.Options.inputFile = this.inputFile;
> 
> 		System.err.println("invoking: " + getLexerDefs.toString());		
> 
> 		Object retObject = getLexerDefs.invoke(null, new Object[0]);
> 
> But all I get is:
> 	Class test.TestParserGenerator can not access a member of class
> parser.LexerDefs with modifiers "public static"
> 
> So loading class seems to work, but I cannot invoke a _public_ method? Why?
> 
> Anyone knows about reflection?

Yes.  Does a public static method with that exact signature "()" exist?
In the classpath?  What if you call it directly?

Andrew.




More information about the fedora-devel-list mailing list