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) {

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.

hi maxarul,

  why don't u use the reflection API to get all the classed in a package
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.
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.
Get your problem seen by more experts

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

Sorry, the first line should read:

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

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


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


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?
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.

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
vaskarthik, that looks like my comment. Very much like my wait up it IS my comment but re-written as your answer.

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(
        at Method)
        at java.lang.ClassLoader.loadClass(
        at sun.misc.Launcher$AppClassLoader.loadClass(
        at java.lang.ClassLoader.loadClass(
        at java.lang.ClassLoader.loadClassInternal(
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

From novice to tech pro — start learning today.