File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes class loader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "class loader" Watch "class loader" New topic
Author

class loader

dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello i m having a problem to load a jar on windows using my class loader here is my code :




It throws a ClassNotFoundException, is there a problem with the path i provide ?
Thank you for helping.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

String urlPath = "jar:file://" + path + "!/";

That will create a URL of the root INSIDE the JAR. You need to add just the JAR. Therefore, remove the "jar:" and "!/".

You can also do this a bit more robust, against possibly troublesome paths, by using File:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Jiang
Greenhorn

Joined: Dec 13, 2007
Posts: 24
Hi,dav mrazek
I don't have commons-dbcp-1.2.2.jar file, so
I change two statements :cl.addFile("C:\\log4j.jar");
Class c = cl.loadClass("org.apache.log4j.Appender");
and execute the program, it works. You can check if commons-dbcp-1.2.2.jar
exists in your c disk.
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello and thanks for your answers, unfortunately it is still not working. I tried removing "jar:" and "!/", and i have the same exception.
Maybe the problem is that im on windows 2000, cause i remember trying this code on Open Solaris and it was working.
James Jiang, i definitely have the jar file on my C: drive.
Which version of windows are you using ?
James Jiang
Greenhorn

Joined: Dec 13, 2007
Posts: 24
oh, it sounds interesting. I am on windows 2003 and it works.
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
I still have the problem on windows 2000, but i payed attention to the exception and it seems that it is a native method problem. Here is the complete exception :

java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at TestClassLoader.main(TestClassLoader.java:27)

I m using netbeans 6 ide and in URLClassLoader i have some forbidden import statements. What does it mean ? thanks for any hints.
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Originally posted by Rob Prime:
String urlPath = "jar:file://" + path + "!/";

That will create a URL of the root INSIDE the JAR. You need to add just the JAR. Therefore, remove the "jar:" and "!/".

You can also do this a bit more robust, against possibly troublesome paths, by using File:


Hello, i came to this problem, and i printed the url to see how it looks like and i have something like this :
url : file:/D:/path/where/the/project/is/ file:/C:/commons-dbcp-1.2.2.jar
so no wonder why it doesn't find anything.
why is my url like this ?
Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class loader
 
Similar Threads
iText: Digital signature
How to add new jar files to class path dynamically at runtime?
how to delete an object from heap ?
getting null values in content encoding attribute in URLConnection class
Class Loaders & Permissions