What classes are in a package? Functional requirement

What classes are in a package? Functional requirement

Post by Glenn McCa » Sat, 06 Dec 2003 09:40:19

What I am trying to achieve is a program that looks for classes in a
particular package.
For each class that it finds in the package I will check that it is derived
from an abstract class (or inteface) that I will define. If it is derived
from my base class I will make it available for a user to use.

Specifically, my program will have a drawing area. If the user "alternate
mouse" clicks on the drawing area, a popup menu will appear with a New item.
The New item will have a list of classes that are found in the package that
I define that are derived from my base class.

From the "user" perspective, to add functionality to my program all they
need to do is extend my base class/interface and declare the class to be
part of the package that I nominate.

Hence the original question can I get a list of classes that are defined in
a specific package?

Obviously other solutions are possible - for example XML file that describes
the classes I can load.
However, I want to try and make the distribution of extensions to be as
simple as compiling a class into a package if I can.


What classes are in a package? Functional requirement

Post by Mark Haas » Sat, 06 Dec 2003 12:12:05

In article < XXXX@XXXXX.COM >,

AFAIK, you cant walk a package hierarchy, you must know all the packages
you want to use by name, in advance. Most plugins work by having a
plugins folder. You can scan the folder for *.class files at init, and
test each one to see if it conforms to the public interface you're
looking for.

|\/| /| |2 |<


What classes are in a package? Functional requirement

Post by Chris Smit » Sat, 06 Dec 2003 12:24:00

They'll also need to put the code somewhere that the program can find
it, right? Otherwise, you're asking for magic from the Java
implementation. You need to start from that angle: where is the code
for these classes? Find them there, and then once you've found the
implementation code, you can turn around and check the package for the
appropriate value if you still want to require that the classes be in a
specific package (but that actually seems like a bad idea to me -
packages do have a purpose).

You'll also find that things get cleaner and easier if you load the
classes in a new class loader (a URLClassLoader with an appropriate
file-scheme URL should work fine).

That question has been answered more times than I can count. Is there
something you don't understand about the answer, or do you just think
the answer will change if you keep asking?

The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

What classes are in a package? Functional requirement

Post by Andrew Tho » Sat, 06 Dec 2003 14:11:19


Your pateince, Chris, is admirable. ;-)

What classes are in a package? Functional requirement

Post by Glenn McCa » Sat, 06 Dec 2003 14:17:46

Thoughts embedded below....

Agreed, but the code supplied will have to be within the scope as defined by
the classpath. Indeed it could be in any directory or set of directories
that correspond to the classpath.
For example if I define a classpath as "/classpath:/~/classpath" and I
define the name of the package that I am going to scan as
"com.mycorp.myapp.plugins" then the extension classes could be in
/classpath/com/mycorp/myapp/plugins/* or
~/classpath/com/mycorp/myapp/plugins/* or as I understand it (although
haven't tried) I could probably have some in the /classpath path and some in
the ~/classpath path.

I was thinking that by adopting this approach, extending my app would be as
simple as compiling a new class into whichever directory on the class path
you like. In this manner there is no restriction to a particular directory -
the plugins simply work if they are placed somewhere in the classpath in the
package I am looking for.

Absoulutely agree with what you say re the use of packagenames - it seemed
reasonable to me to define a "plugins" package for my app to organise them.

I shall explore that - thanks


Not really, I was providing more specific information in the hope someone
might say well you can't do exactly that but you could do this.
I am thinking the way I am going to have to tackle this is either restrict
to a particular directory or scan the directories listed in the classpath in
conjunction with my package name looking for .class files.

Shame, Java does so much, I thought/hoped it could do this as well.

What classes are in a package? Functional requirement

Post by Dale Kin » Sun, 07 Dec 2003 07:21:40

Glenn McCall" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
directory -

It would have to be psychic to be able to do it completely.

I might suggest another alternative that works the way that JavaBeans do.
The JavaBeans spec allows you to package plugins into a jar and you know
what bean classes are in the jar because they are listed in the manifest
file for the jar:


So the creator of the plug-in has to create a special jar file to indicate
which classes to look for in the jar. To use a plugin in your program the
user either selects the plug-in jars to use and/or puts them in a plug-ins

A slight variation on this is also defined in the Jar spec called the
service provider. All service provider defines is that you can put files
into a subdirectory of the META-INF directory.

Generating the files for the META-INF directory can be automated using the
xDoclet tool:



What classes are in a package? Functional requirement

Post by Glenn McCa » Fri, 12 Dec 2003 08:38:16

I just can't resist.


And yet my perserverence got much more than the original response from Chris
which was simply