Inspecting a package

Posted on 2002-04-10
Last Modified: 2010-03-31

I need to inspect all the classes of a certain package (for example java.util).
I was looking for a piece of code like
Classes [] classes = new Package("java.util").getClasses()

but this method does not exist.
Anyone knows how can i do it?
Question by:ebuchner
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 92

Expert Comment

ID: 6932662
No easy way to do that. The class loader only loads classes on demand, so until a class is needed it knows nothing about it.
LVL 35

Expert Comment

ID: 6932773
 Objects is right, it is not easy to do it. What I can think off the top of my head is to write your customized class loader that goes through a given subdirectory (package) recursively and loads all the classes. Then you can use reflection to get the properties and methods of the classes.

 Hope it helps.

Accepted Solution

Igor Bazarny earned 200 total points
ID: 6933338

If you are using default classloader, you can use java.class.path, sun.boot.class.path and java.ext.dirs system properties and,* classes to list .class files there.

Something like that (just an idea):

String package = "java.util"
String packageDir = package.replace('.',File.separatorChar);
String packageInZip = package.replace('.', '/')+"/";
ArrayList classes = new ArrayList();

StringBuffer buffer = new StringBuffer(System.getProperty("sun.boot.class.path"));

StringTokenizer extTokens = new StringTokenizer(System.getProperty("java.ext.dirs"), File.pathSeparator);
    File root = new File(extTokens.nextToken());
    File[] list = root.list();
    for(int i=0; i<list.length; ++i){
        if( list[i].getName().endsWith(".jar") ){
StringTokenizer pathTokens = new StringTokenizer(buffer.toString(), File.pathSeparator);

    File entry = new File(pathTokens.nextElement());
    if( entry.isDirectory() ){
        File dir = new File(entry,packageDir);
        if( dir.isDirectory() ){
            String[] list = dir.list();
            for( int i=0; i<list.length(); ++i){
                if( list[i].endsWith(".class") ){
        ZipFile zip = new ZipFile(entry);
        Enumeration zipEntries = zip.entries();
            String current = ((ZipEntry)entries.nextElement()).getName();
                String relative = current.substring(packageinZip.length());
                if( relative.indexOf("/") < 0 && relative.endsWith(".class")){

Igor Bazarny,
Brianbench MVP for Java 1


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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 135
going to wrong jsp page 2 70
Selenium docs api java index 3 98
collection output issue 9 96
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses

752 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