Tuesday, October 21, 2008

Loading Jars Dynamically

Post after really long time..from now on ..i ''ll be frequently updating this with random but interesting java posts.
Earlier this week i had a requirement to dynamically load some classes in the current JVM's classpath from some external Jar files.
The way to load classes dynamically from any particular location is to use a java.net.URLClassLoader.
Lets see some code.

//Here u specify the URL for a jar on the file system
String filePath = "jar:file:///" + sysfilePath + "!/";
//Make a URL out of It.
URL url = new URL(filePath);
//Use the ClassLoder to Load that Jar
//You can pass in as many URL you want to the classloader
URLClassLoader clazzLoader = new URLClassLoader(new URL[]{url});


I am thinking to build a fluent interface on this classloader which will allow me to dynamically load more jars as the application goes along.

While researching and googling for this, i upon this JCL.Worth a try.

Do you have any recommendations or alternative approaches?Do comment below.

0 comments:

Sociofluid