has a way to get all classes in a package?

A

aeonsun

I can get the package by Package.getPackage("example"),but i can't get
the classes in the package.
thx!
 
A

aeonsun

I will get the classes dynamically,because i didn't know which new
Class has been add in to the package.
 
M

Martin Lansler

Don't think this is possible since the normal java classloader is lazy, i.e.
it only loads classes that are needed... There is no record over all
'avalable' classes.
Perhaps you could try to write your own classloader that scans through JVM
classpath.

Regards,
Martin.
 
P

Paul Cager

aeonsun said:
I will get the classes dynamically,because i didn't know which new
Class has been add in to the package.

There isn't a foolproof way to do this, since classes can be loaded from
so many different sources - class files in directories, JARs, URLs etc.
And you could always write your own ClassLoader which might load classes
in a completely new way.

However if you know that your classes will always be in directories
listed in the CLASSPATH then you could use java.io.File to do the
searching - although this will be messy, fragile code. I believe there
is some "standard" tool to do it - have a google of this newgroup and
c.l.j.help as this question comes up quite regularly.

But as Jean-Francois Briere said, "Why do you want to do it?" There are
often better ways, such as using a properties file.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top