G
Glenn McCall
Given a package name, is it possible to determine (via Java code) what
classes are defined within a package?
classes are defined within a package?
Glenn said:Given a package name, is it possible to determine (via Java code) what
classes are defined within a package?
If you have a main class name, you can fetch that class, the study theGiven a package name, is it possible to determine (via Java code) what
classes are defined within a package?
Glenn McCall said: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.
TIA
Glenn said: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?
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?
Chris Smith said: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?
Glenn McCall said: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
defined
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.
Andrew Thompson said:.. defined
Your pateince, Chris, is admirable. ;-)
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.