aspose file tools*
The moose likes Beginning Java and the fly likes Adding jar to the classpath - dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding jar to the classpath - dynamically" Watch "Adding jar to the classpath - dynamically" New topic
Author

Adding jar to the classpath - dynamically

Arvind Kumar Patel
Greenhorn

Joined: Mar 11, 2006
Posts: 24
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Arvind Kumar Patel
Greenhorn

Joined: Mar 11, 2006
Posts: 24
Found a solution, the AddURL method in URLClassLoader works perfectly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding jar to the classpath - dynamically
 
Similar Threads
why include directory in classpath ?
struts-core.jar
Jar file exception due to not finding Oracle driver but other standalone programs can use it
Newbie: Run-time error
Jar file loading....