0

I need to get all the .class files located in a .jar file which in turn is located within another .ear file.

I am trying to use the JarInputStream to get access to the .ear. This is working ok. However, when I iterate the JarEntry elements inside the JarInputStream, I can't seem to be able to open it as a Jar file in order to read any .class elements within it.

JarInputStream jarFile = new JarInputStream(new FileInputStream("c:/path/to/my/.ear"));

JarEntry jarEntry = null;

while(true) {

    jarEntry = jarFile.getNextJarEntry();

    if(jarEntry == null) {
        break;
    }

    if((jarEntry.getName().endsWith(".jar"))) {
        //Access to the nested jar?
    }
}

Edited: worth mentioning that the code above is in the same jar as the classes I am trying to find programmatically.

  • 1
    I think you should take a look at http://stackoverflow.com/questions/1176859/jarfile-from-inside-a-jar-or-inputstream-to-file. – javanna Mar 15 '11 at 19:57

2 Answers2

3

You should first extract the .jar file from the .ear file and then try to read the class file.

In fact, that's how even the Application Servers do. They extract the EAR file into a temporary directory and then load the classes from there.

adarshr
  • 61,315
  • 23
  • 138
  • 167
2

Did you try this?

[...]    
    if((jarEntry.getName().endsWith(".jar"))) {
        JarInputStream subJarStream = new JarInputStream(jarFile);
        // the same search again
    }
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210