• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class loader

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic