Classes in a package

Posted on 2000-01-21
Medium Priority
Last Modified: 2010-04-01
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) {

Question by:maxarul

Expert Comment

ID: 2374487
hi maxarul,

  why don't u use the reflection API to get all the classed in a package

Author Comment

ID: 2374508
How do you suppose I do that ?

The Package Object does not have any methods which return all the classes in the package.
LVL 16

Expert Comment

ID: 2374592
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.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

LVL 16

Expert Comment

ID: 2374725
Sorry, the first line should read:

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


Expert Comment

ID: 2376262
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?

Accepted Solution

vaskarthik earned 100 total points
ID: 2376881
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.

Expert Comment

ID: 2377291
vaskarthik, that looks like my comment. Very much like my comment...no wait up it IS my comment but re-written as your answer.


Expert Comment

ID: 11749518
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)

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month6 days, 12 hours left to enroll

592 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question