Classes in a package

How do I programmatically get all the CLASSES in a given package x.

I need the create a method with the following signature

Class[] getClasses(Package x) {

}
maxarulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sivasundaramCommented:
hi maxarul,

  why don't u use the reflection API to get all the classed in a package
0
maxarulAuthor Commented:
How do you suppose I do that ?

The Package Object does not have any methods which return all the classes in the package.
0
imladrisCommented:
The JVM does not have prior knowledge of all existing methods since Java is dynamically loaded. Unlike other languages the "objects" (class files) generated from Java source files are not precombined into one large "executable". Instead, they are loaded as needed. One reason for this is to "hide" the loading time from Internet users. This way, an applet is loaded in little chunks. Also, any piece of the program that is not executed is not loaded at all.

For these reasons there is no "knowledge" within the Java environment about all available classes. It goes and finds one when it turns out it is needed.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

imladrisCommented:
Sorry, the first line should read:

The JVM does not have prior knowledge of all existing CLASSES since




0
JodCommented:
There is a way to do this under some circumstances.


Every package is stored in a particular directory of the same name as the package.

To find all the classes in the package you just need to find that directory.

On a development machine you can look through the java class path and find the matching dir.

For example, a package called

  mypackackage.pack1

will be stored in a dir path accessible to the compiler and runtime named

  .../mypackackage/pack1

Find this dir and you can just read the path names from the files named *.class (trying not to get confused by inner classes...).

For jar files you would have to open up the jar that the package is in and read off the filenames from that. Not hard to do.


For applets you can follow the applet Java codebase parameter to see where the source files are located and again read through them to find all the ones ina a package. You will have access to do this from an Applet as you are just looking at the server that the applet was downloaded from.

Dos this help at all?
0
vaskarthikCommented:
You can use the File class methods to list all the classes in a particular package. Basically the package is represented as an directory in the HD and all the classes are stored as files under that particular (package)directory. so you can refer File class for listing the classes.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JodCommented:
vaskarthik, that looks like my comment. Very much like my comment...no wait up it IS my comment but re-written as your answer.

Intersting...
0
cuppsCommented:
when i create my package name on my linux box under the tomcat root directory. i complile and it runs but when i run it i get error below

Exception in thread "main" java.lang.NoClassDefFoundError: CreateMinorityTables (wrong name: products/xtremedesignz/src/CreateMinorityTables)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.