• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class loader

 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
James Jiang
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, it sounds interesting. I am on windows 2003 and it works.
 
dav mrazek
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic