0

I'm looking for a way to obtain the full list of all classes and packages available in Java runtime.

I've found many solutions which print only the list of the ".jars" or the folders containing classes loaded in the classpath, but this is not enough for me, I need the list of available classes.

How to do this?

Alessandro C
  • 3,310
  • 9
  • 46
  • 82

1 Answers1

-1

Below code will give you list of class name (fully qualified) for the external jar you pass as a path

package Sample;

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;

    public class Sample {
        public static void main(String[] args) throws IOException {
            List<String> classNames = new ArrayList<String>();
            ZipInputStream zip = new ZipInputStream(new FileInputStream("C:\\Users\\foo\\Desktop\\foo.solutions.jar"));
            for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
                if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
                    // This ZipEntry represents a class. Now, what class does it represent?
                    String className = entry.getName().replace('/', '.'); // including ".class"
                    classNames.add(className.substring(0, className.length() - ".class".length()));
                }
            }
            System.out.println(classNames);
        }
    }
Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37