Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding JAR file to Classpath at Runtime

 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Is there any way to load a jar file into the Classpath during the execution of an application?

Thanks.
 
Raj Chila
Ranch Hand
Posts: 128
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raj Chila
Ranch Hand
Posts: 128
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, now I got it. Thanks again.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic