| Author |
Adding JAR file to Classpath at Runtime
|
Yohan Liyanage
Ranch Hand
Joined: Aug 17, 2007
Posts: 132
|
|
Hi guys, Is there any way to load a jar file into the Classpath during the execution of an application? Thanks.
|
Yohan Liyanage
http://blog.yohanliyanage.com
|
 |
Raj Chila
Ranch Hand
Joined: Mar 18, 2004
Posts: 125
|
|
|
You can do this using URLClassLoader, you can get a handle to this through the static getSystemClassLoader() in the ClassLoader class. then add the jar file that you want as a URL and then load that class.
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
I'd be interested in an example of that because I don't see how to do it. More commonly, create a new URLClassLoader (or a custom subclass) with the new URL in the constructor arguments. Use that loader to load some anchor for your app, and anything from there down (if you do nothing else to break the chain) will have the same classpath.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Raj Chila
Ranch Hand
Joined: Mar 18, 2004
Posts: 125
|
|
I actually used it in one of my projects...and there is a correction to my earlier post. Actually I created a Sub Class of the URLClassLoader and then create an instance of it. Here is the example any way. MyClassLoader Sorry for the confusion. [ November 20, 2007: Message edited by: Raj Chila ]
|
 |
Yohan Liyanage
Ranch Hand
Joined: Aug 17, 2007
Posts: 132
|
|
Thanks a lot guys ! By the way, what is the purpose of subclassing URLClassloader ? Can't we use instead ? [ November 20, 2007: Message edited by: Yohan Liyanage ]
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
Originally posted by Raj Chila:
Well, i see one big problem with the code above. It assumes that the system class loader is always a URLClassloader! This can be extremely risky considering the fact that anyone can change the system class loader using the property "java.system.class.loader". Moreover, there is no need to get all the urls from the system class loader. As Stan said, just create a new URLCLassloader with the new URL. In case this classloader does not find the specified class in the specified url, it will delegate the class loading to its parent.(By default the parent is the system class loader) [ November 21, 2007: Message edited by: Nitesh Kant ] [ November 21, 2007: Message edited by: Nitesh Kant ]
|
apigee, a better way to API!
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
[B][Yohan]: Thanks a lot guys ! By the way, what is the purpose of subclassing URLClassloader ? Can't we use instead ?[/B] The addURL() method is protected, not public. So you would need a subclass to access it. However as Stan indicated, it's more common to simply create a new URLClassLoader that has the desired URL in the constructor.
|
"I'm not back." - Bill Harding, Twister
|
 |
Yohan Liyanage
Ranch Hand
Joined: Aug 17, 2007
Posts: 132
|
|
|
Hmm, now I got it. Thanks again.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Originally posted by Nitesh Kant: As Stan said, just create a new URLCLassloader with the new URL. In case this classloader does not find the specified class in the specified url, it will delegate the class loading to its parent.(By default the parent is the system class loader)
Actually, it's the other way around. If the parent can't find it, then it tries to find it itself. From the API:
When requested to find a class or resource, a ClassLoader instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Adding JAR file to Classpath at Runtime
|
|
|