Changing the Classpath at runtime

B

Brian Andersen

I need to be able to change (really add to) the classpath after my main
application is running. Can anyone tell me how to do this?

Any help would be appreciated

Brian Andersen
 
M

Marco

Brian said:
I need to be able to change (really add to) the classpath after my main
application is running. Can anyone tell me how to do this?

I've had this problem before. The trouble is that the "System"
class loader (the default class loader for "user" classes that
have no affiliation with the standard Java API) never re-reads
the "java.class.path" system property after the JVM starts...

System.getProperty("java.class.path");

I may be wrong, but the conclusion I reached is that you have
to write your own custom ClassLoader that dynamically reads
(and re-reads) the value of "java.class.path" throughout its
runtime, and resolves class byte streams relative to that.

Marco
 
D

Derek Clarkson

Hi Brian,
(Haven't tried this) The System class has the ability to get and set system
properties. One of the available properites (look at the getProperties()
method for a list) is the class path. It looks like you can get it, append
the paths you want, and then set it.

cio
Derek.
 
G

Guest

I need to be able to change (really add to) the classpath after my main
application is running. Can anyone tell me how to do this?

Any help would be appreciated

Brian Andersen

You may want to try a URLClassLoader (or a custom subclass of that).

Mele Kalikimaka,
La'ie Techie
 
D

David Zimmerman

Marco said:
I've had this problem before. The trouble is that the "System"
class loader (the default class loader for "user" classes that
have no affiliation with the standard Java API) never re-reads
the "java.class.path" system property after the JVM starts...

System.getProperty("java.class.path");

Something I've done is to arrange for a particular directory to be on
the classspath and get the application to put the new classes in that
directory. (You have to put them in there before you try to load them
and fail as the failure will be cached.) Then the app can
Class.forName() them up.
 
A

Andrew Thompson

La?ie Techie said:
You may want to try a URLClassLoader (or a custom subclass of that).

The OP specifically mentioned 'main', but I
will just add for any interested that the URLClassLoader
will throw SecurityAccessExceptions if you try to do this
in an Applet, ..unless you have specifically added
the extra paths to the 'archive' tag of the applet
(which negates the 'after start' bit), or sign the Applet.
 
D

Darren

Brian said:
I need to be able to change (really add to) the classpath after my main
application is running. Can anyone tell me how to do this?

Any help would be appreciated

Brian Andersen

As another option you could consider writing your own classloader (far
easier than it sounds) as presumably you know the location of the
classes you need to load after the application has started.

Darren
 
T

Tor Iver Wilhelmsen

Darren said:
As another option you could consider writing your own classloader (far
easier than it sounds) as presumably you know the location of the
classes you need to load after the application has started.

The java.net.URLClassLoader is a "catch-all" sort of class loader that
you can pass URLs to. Just call addURL() whenever you need to add
something to the classpath - but keep in mind it appends to the list,
so you can't use it to "replace" classes on the preset classpath
(passed to the constructor or factory method).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top