Java classes - conspicuous by their absence.

T

taylorius

I have an applet, which processes a data file.

Depending on the contents of that data file, certain classes of that
applet
will be needed, or not. Furthermore, I want to mimize the size of the
applet by tailoring it to a particular data file, including only the
classes that will actually be used by that data file in the jar.

No problem I hear you say, just instantiate the class on a contitional
and away you go. Well, on a pc (ms and sun jvms) this is true. But on
the Apple Mac (who would've thought it!) referring to a non-existant
class anywhere, causes an exception as soon as the referring class is
instantiated, even if the problem code would never be executed.

So I'm wondering if anybody can think of an easy way to do this. I
suppose I could use reflection, but it seems like overkill.

Many thanks for any ideas

Matthew Taylor
 
M

Matt Humphrey

taylorius said:
I have an applet, which processes a data file.

Depending on the contents of that data file, certain classes of that
applet
will be needed, or not. Furthermore, I want to mimize the size of the
applet by tailoring it to a particular data file, including only the
classes that will actually be used by that data file in the jar.

No problem I hear you say, just instantiate the class on a contitional
and away you go. Well, on a pc (ms and sun jvms) this is true. But on
the Apple Mac (who would've thought it!) referring to a non-existant
class anywhere, causes an exception as soon as the referring class is
instantiated, even if the problem code would never be executed.

So I'm wondering if anybody can think of an easy way to do this. I
suppose I could use reflection, but it seems like overkill.

Take the class "Thing" you want to defer loading for, rename it as
"ThingImpl". Redefine "Thing" as an interface (or abstract class) with all
the same methods, etc. (Don't otherwise change your code, as it will already
be using Thing) When you're ready to create the object, just say:

Thing thing = (Thing)Class.forName ("ThingImpl").newInstance ();

Be sure you use the fully qualified package name of the class.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top