Loading classes at run time

M

Mike

Hi,

I have my program in jar file and it work's. However, when I try to
add new functionality to dynamically load classes at runtime, my
program doesn't find the classes that I'm trying to dynamically load.
In the first case, if my classes are in a directory in the system
CLASSPATH, what code do I need in my program (which is inside a jar
file.) to dynamically load those classes specified at run time. In the
second case, if my classes are inside a jar file in a directory in the
system CLASSPATH, what code do I need this case. (Or must I hardwire
the classes paths into my program jar's manifest file.)Can someone
provide me with a sample? I'm very confused about this and I spent
several days and I can't get it to work.
 
A

Arne Vajhøj

Mike said:
I have my program in jar file and it work's. However, when I try to
add new functionality to dynamically load classes at runtime, my
program doesn't find the classes that I'm trying to dynamically load.
In the first case, if my classes are in a directory in the system
CLASSPATH, what code do I need in my program (which is inside a jar
file.) to dynamically load those classes specified at run time. In the
second case, if my classes are inside a jar file in a directory in the
system CLASSPATH, what code do I need this case. (Or must I hardwire
the classes paths into my program jar's manifest file.)Can someone
provide me with a sample? I'm very confused about this and I spent
several days and I can't get it to work.

No matter where you classes are in classpath, then you load them
with Class.forName !

(actually you also do even if they ar enot in classpath, then you just
need to specify your own classloader)

Arne
 
M

Mike

No matter where you classes are in classpath, then you load them
with Class.forName !

(actually you also do even if they ar enot in classpath, then you just
need to specify your own classloader)

Arne

But that doesn't work from inside a jar file. That's the problem that
I'm trying to solve.
 
A

Arne Vajhøj

Mike said:
But that doesn't work from inside a jar file. That's the problem that
I'm trying to solve.

Class.forName works with both the loading and loaded class
in a jar file.

Arne
 
A

Andrew Thompson

Class.forName works with both the loading and loaded class
in a jar file.

Here is a snippet from Appleteer, that loads applets.

<snippet>
/* Create an instance of an applet, hook it to an
artificial stub and a layout, then launch it. */
public void loadApplet() throws Exception {

Class clas = urlcl.loadClass(
element.getCode() );

applet = (Applet)clas.newInstance();

applet.setStub(stub);

add(applet, BorderLayout.CENTER);

browser.
getAppletContext().
addApplet(
element.getName(), applet
);
applet.init();
//applet.setFocusable(true);
validate();
}
</snippet>

The URLClassLoader (urlcl) consists of either
URLs made from the applet 'archive' attribute,
or failing that the 'codebase', or failing that,
the document base.

It works just fine with both Jar'd and
loose classes.
 
M

Mike Schilling

Mike said:
But that doesn't work from inside a jar file. That's the problem
that
I'm trying to solve.

The usual thing that people mean by "loading classes at run time" does
work when the classes are in a jar file. Unless you can provide an
example (complete with classpath) that shows the failure, I suspect
we'll continur to talk past each other.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top