Adding JARs to the classpath at runtime?

I have an JAVA application with a plugin-architecture. Each plugin is described in a config file that tells the application in which JAR the plugin is found etc.

My problem is this: some plugins need additional JARs on the classpath. E.g. the plugin might need the mysql jdbc JAR on the classpath.

What do I have to do to dynamically add a couple of JAR URLs to the classpath so that the system class loader can automatically load from these additional JARs?

[479 byte] By [johann_pa] at [2007-11-27 8:51:25]
# 1

If you're writing a pluggable application, and you want to hot-plug modules, you'd be best advised to use your own classloaders to load them, too. That's the bestest way to add new plugins at runtime. That's really to large a subject to tackle in a forum to any degree of satsifaction, though. Basically, you want to subclass URLClassLoader, which has a protected method called addUrl, that allows you to do exactly what you're after. Google is full of info on doing this

Another avenue to look at, depending on how far into your project you are, is something such as OSGi, which is already a nice sturdy plugin architecture. The Eclipse Equinox platform is one implementation of OSGi, and Eclipse itself has wizards that help write plugins. You could look at that, it'll be a lot more stable and flexible than anything you can write yourself in a reasonable time-frame

georgemca at 2007-7-12 21:04:37 > top of Java-index,Java Essentials,New To Java...
# 2

> If you're writing a pluggable application, and you

> want to hot-plug modules, you'd be best advised to

> use your own classloaders to load them, too. That's

> the bestest way to add new plugins at runtime. That's

> really to large a subject to tackle in a forum to any

> degree of satsifaction, though. Basically, you want

> to subclass URLClassLoader, which has a protected

> method called addUrl, that allows you to do exactly

> what you're after. Google is full of info on doing

> this

>

This is actually what I am doing and it works to some degree.

Something I must doing wrong though: for example, one plugin

uses the java scripting engine to embed Groovy scripting.

For this, I add the JARs that contain the Groovy scrupting engine and

the groovy stuff using addUrl.

However, when I create the scripting engine, it is the

javax.scripting.* classes who internally want to load the groovy

engine and other classes and they do not seem to be able

to access the JARs that I have added with addURL to my own

classloader.

When I add those JARs to the classpath before running my application

everything works just fine.

So the problem seems to be that the JARs added with addURL are

not accessible to whatever mechanism the scripting engine

initializer (or the JDBC library) uses to load all the classes it needs.

The system class loader is set as the parent of my own class loader.

johann_pa at 2007-7-12 21:04:37 > top of Java-index,Java Essentials,New To Java...