Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading Classes From A Jar File Problem

 
Josh Lam
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to load classes from a jar file using URLClassLoader
using the following code:
void loadAClass(String className){
URL aUrl = new URL("jar:file:/D:/App/ajar.jar!/");
URL[] urls = {aUrl};
URLClassLoader jarClassLoader = new URLCLassLoader(urls);
Class c = jarClassLoader.loadClass(className);
}
Suppose in the jar file I have a class called X.class and the fully qualified name is: A.B.X.class
What should the format of 'className' be when I invoke loadClass()? Should it be A.B.X.class or A/B/X.class. Either one I tried I get java.lang.ClassNotFoundException.
What am I doing wrong?
 
Josh Lam
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to answer my own question:
The answer is:
loadAClass("A.B.X");
Note: the '.class' suffix is not needed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic