André Stannek
André Stannek

Reputation: 7863

members of java class loaded via class loader uninitialized

I am loading a java Class from a jar file using an URLClassLoader.

String className = ...
File jarFile = ...
ClassLoader parent = this.getClass().getClassLoader();
ClassLoader urlLoader = new URLClassLoader(new URL[] { jarFile.toURI().toURL() }, parent);
Class <?> loadedClass = urlLoader.loadClass(className);

This works so far but all the member of loadedClass (like declaredMethods, etc...) are null after the loading. I guess that is the intended behavior and all the members are loaded dynamically when used. Is there a way to initialize them all right away?

Upvotes: 0

Views: 109

Answers (1)

Philipp Wendler
Philipp Wendler

Reputation: 11433

How do you access the members of loadedClass? The private fields will get initialized the first time the corresponding getter method is called, so you should never get a null from these getters.

If you do use these getters and they work as expected, why would you want to initialize all of the members eagerly? This seems just like unnecessary work.

Upvotes: 1

Related Questions