Search code examples
javajava-compiler-api

How to copy a JarEntry to a directory


I have scanned the maven directory (.m2) and have filtered all the files ending with .jar.

List<File> jarFiles = scanRecursivelyForJarObjects(mavenRepository, fileManager);

I am trying the copy the .class files inside the JarFile to a different directory. Here is the code:

for(File jarFile : jarFiles) {
        Enumeration<JarEntry> enumeration = new JarFile(jarFile).entries();
        while (enumeration.hasMoreElements()) {
            JarEntry jarEntry = enumeration.nextElement();
            if (!jarEntry.isDirectory() && jarEntry.getName().endsWith(".class")) {
                copyFile(new FileInputStream(jarEntry), classesDir.getAbsolutePath() + "\\" + jarEntry.getName());
            }
        }
    }

But it is throwing an error when I try to copy because jarEntry is not a File.

I am not sure how to convert jarEntry to a File.


Solution

  • You can get the input stream to the entry.

    new JarFile(file).getInputStream(jarEntry)