listing of all classes in a package/directory -- J2EE

U

ufmemo

I have what seems to me to be a simple question -- but everything I've
tried hasn't worked.

What I would like to do is the following:

In a J2EE (Websphere) app I am building a framework where I need to
dynamically instantiate and run specific methods on all of the classes
in a package. I can guarantee that 1) I know the package name 2) there
will be one or more classes in this package but will not know how many
until run time 3) that all classes will implement a specific interface.


I could do this by using Java I/O getting the listing of the directory
that houses these classes, and then iterate over this list dynamically
instantiating these classes and invoking the methods defined by the
interface. The problem is that since I am doing this in the J2EE world
I have no idea where this package will live when deployed, and
consequently where these classes will live. I have looked at the
possibility using the classloader as a possible way to list all the
classes in a package, java i/o and the servlet context to see if I can
get a directory listing of the package that way, and even Jakarta
discovery all to no avail.

Does anyone have a suggestion for me? Thanks in advance.
 
O

Oscar kind

I have what seems to me to be a simple question -- but everything I've
tried hasn't worked.

What I would like to do is the following:

In a J2EE (Websphere) app I am building a framework where I need to
dynamically instantiate and run specific methods on all of the classes
in a package. I can guarantee that 1) I know the package name 2) there
will be one or more classes in this package but will not know how many
until run time 3) that all classes will implement a specific interface.

First, a search of the newsgroups will tell you there is no way to do this
using classloaders. This is because classloaders are not restricted in how
they load classes: they may even generate them on the fly. It is thus
impossible to tell which classes any given classloader may load.

I could do this by using Java I/O getting the listing of the directory
that houses these classes, and then iterate over this list dynamically
instantiating these classes and invoking the methods defined by the
interface.

This is the core of the problem: you must bve able to get a list of
classes from the same source the classloader will use. Given that a
classpath may consist of several sources, your best bet is to document and
require one of them, and locate the classes there.

The problem is that since I am doing this in the J2EE world
I have no idea where this package will live when deployed, and
consequently where these classes will live.

One thing you can do is support a limited number of application servers
and/or servlet containers. AFAIK, you can at least identify Tomcat and
Resin with some probability by the existence of the "tomcat.home" or
"resin.home: system properties. You must then hope that they use a default
installation. This doesn't sound hopeful.

Another option is to require the setup to have a readable root in the
classpath, which is pointed to by a configuration parameter. This root
(probably a directory of a .jar file) is then where the classes are
expected.

I have looked at the
possibility using the classloader as a possible way to list all the
classes in a package, java i/o and the servlet context to see if I can
get a directory listing of the package that way, and even Jakarta
discovery all to no avail.

Yes, this is because the concept of a classloader makes no requirements as
to how classes are loaded. As there is no guarantee that it is known in
advance which classes can be loaded, you cannot get this information.
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top