0

My current task is to iterate a .jar to see if it has .jars inside. Then I have to get those .jars and check certain file in each one.To do that, I am using JarFile to get the JarEntries. The problem is, How do I do to get again the JarFile from the current Jar Entry?. Is it possible?

Jar:
 Jar1:   
   /META-INF/source/file1.txt
 Jar2: 
   /META-INF/source/file1.txt

Here is my current code:

 public static void main(String[] args) throws IOException,   URISyntaxException {

    String dir = Paths.get(".").toAbsolutePath().normalize().toString() + "/target";
    Collection<String> jarBuildList = JarFinder.getJars(dir);
    for(String buildJar : jarBuildList){

        File file = new File(buildJar);
        JarFile buildJarFile = new JarFile(file);

             Enumeration<JarEntry> entries = buildJarFile.entries();
             while (entries.hasMoreElements()) {
                JarEntry jarEntry = entries.nextElement();
                if (jarEntry.getName().contains(".jar")){
                      //Having the JarEntry, enter to that jar javaEntry and get a file1.txt
                }                       
             }              
        }

    }
}
  • extract the jar file inside, with this: http://stackoverflow.com/questions/1529611/how-to-write-a-java-program-which-can-extract-a-jar-file-and-store-its-data-in-s and do it again – guillaume girod-vitouchkina Dec 11 '15 at 23:25
  • Why not convert it to a zip, extract the files in there, if those files are a jar, repeat. – Andre Dec 11 '15 at 23:25

1 Answers1

0

How do I do to get again the JarFile from the current Jar Entry?. Is it possible?

Not directly, no. Your alternatives are:

  • copy the content of the entry to a file in the file system and use JarFile to open that, or

  • use the JarInputStream API.

FWIW, the reason that JarFile only works on files in the file system is that its implementation is a native library which uses native code to open and read the source file. It was done this way (I think) because loading from JAR / ZIP files has to work before the JVM is fully bootstrapped. Then the API became frozen ... for the normal reasons.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216