3

I just made a program, which iterates through a jar file, and prints out all of the qualified path names for each .class file

My code looks like the following:

public static ArrayList<String> getClassNames(String jarName) {
    ArrayList<String> CorbaClasses = new ArrayList<String>();

        System.out.println("Jar " + jarName );
    try {

        JarInputStream jarFile = new JarInputStream(new FileInputStream(
                jarName));
        JarEntry jarEntry;

        while (true) {
            jarEntry = jarFile.getNextJarEntry();
            if (jarEntry == null) {
                break;
            }
            if (jarEntry.getName().endsWith(".class")) {

                    System.out.println("Found "
                            + jarEntry.getName().replaceAll("/", "\\.").replace(".class", ""));
                CorbaClasses.add(jarEntry.getName().replaceAll("/", "\\."));
                jarFile.close();
            }
        }
        jarFile.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return CorbaClasses;

}

What I need now is not to iterate only through one jar file, but to iterate through a bunch of folders, where in each folder there is a jar file, that I need to print out the path names of the class files.

How can i do that?

phil652
  • 1,484
  • 1
  • 23
  • 48
Stefan
  • 49
  • 1
  • 1
  • 3
  • Check this out: http://stackoverflow.com/questions/3154488/best-way-to-iterate-through-a-directory-in-java – Ian2thedv Apr 21 '15 at 13:41

1 Answers1

0

to iterate over multiple directories/subdirectories use the following code.

File path = new File("your root Directory path");
File [] files = path.listFiles();//return list of all folder plus file in root directory
    for (int i = 0; i < files.length; i++){
        if (files[i].isFile()){ //if it is file do something
            System.out.println(files[i]);
        }else {
            //it is directory go inside to find jar files
            //if inside also directory found make it function and call it recursively
        }
    }
Mitul Sanghani
  • 230
  • 1
  • 11