Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding jar to the classpath - dynamically

 
Arvind Kumar Patel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a bunch of jar files in a /directory.

I want to add them to the classpath at runtime.

I can load standalone classes, but how can it be done for jar files without being too cumbersome?

Thanks.

P.S. I looked at URLClassLoader, but it requires you to call "loadClass" to load a user supplied class. I want to load *all* classes without instantiating a particular one, as if it were added using the "-cp" switch.
[ March 23, 2006: Message edited by: Arvind Kumar Patel ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you will find a way to do this with a simple function call from the API. However, you should be able to write a function that does this. I would start by looking at the classes in java.util.jar. You should be able to use these classes to iterate over the .class files in a jar file and load each one individually with URLClassLoader.

HTH

Layne
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arvind Kumar Patel:
I looked at URLClassLoader, but it requires you to call "loadClass" to load a user supplied class. I want to load *all* classes without instantiating a particular one, as if it were added using the "-cp" switch.


Specifying a Jar on the classpath does not cause all its classes to be loaded. It just makes them available, should they need to be loaded.
 
Arvind Kumar Patel
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found a solution, the AddURL method in URLClassLoader works perfectly.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic