This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Loading classes from a Jar file problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Loading classes from a Jar file problem" Watch "Loading classes from a Jar file problem" New topic
Author

Loading classes from a Jar file problem

Josh Lam
Greenhorn

Joined: Feb 16, 2000
Posts: 19
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

Joined: Feb 16, 2000
Posts: 19
I am going to answer my own question:
The answer is:
loadAClass("A.B.X");
Note: the '.class' suffix is not needed.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Loading classes from a Jar file problem
 
Similar Threads
Regarding ClassLoader concept
loading JDBC driver from a Jar (not in classpath)
Loading Classes From A Jar File Problem
URLClassLoader locking the jars
Loading POJO object without loading library first