javaeclipse-rcpclasspath7zipeclipse-classpath

Getting an exception when trying to un-zip the 7-Zip Archive (in binary mode)


I'm running a Java application in Eclipse. Here, I'm trying to Unzip a 7-Zip Archive file using the following code :

SevenZFile sevenZFile = new SevenZFile(new File(localPath));
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        while(entry!=null){
            tempExtractFilePath = extractFilePath + entry.getName();
            FileOutputStream out = new FileOutputStream(tempExtractFilePath);
            if(!patchOutputList.contains(tempExtractFilePath)) {
                patchOutputList.add(tempExtractFilePath);
            }
            byte[] content = new byte[(int) entry.getSize()];
            sevenZFile.read(content, 0, content.length);
            out.write(content);
            out.close();
            entry = sevenZFile.getNextEntry();
        }
sevenZFile.close();

While executing the above set of lines in debug mode (running withing Eclipse) I'm able to unzip the 7-zip files (and no issues).

But when trying to do the same in binary mode (after exporting the Java application into a .JAR file) I'm encountering the following exception trace :

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/sevenz/SevenZFile
    at com.ami.veb.core.patch.PatchInfo.unZipPatchFile(PatchInfo.java:1047)
    at com.ami.patch.patchinspector.PatchInspector.applyPatchesLocally(Patch
Inspector.java:536)
        at com.ami.patch.patchinspector.PatchInspector.main(PatchInspector.java:
198)
Caused by: java.lang.ClassNotFoundException:   org.apache.commons.compress.archive
rs.sevenz.SevenZFile
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        ... 3 more

I've also added the following 2 lines into the .classpath of the Java application

<classpathentry exported="true" kind="lib" path="lib/commons-compress-1.9.jar"/>
<classpathentry exported="true" kind="lib" path="lib/xz-1.5.jar"/>

but still it doesn't resolve the problem.

Any help will be greatly Appreciated !!


Solution

  • You dont need to write it manually

    try export Runnable Jar File / with option "Package required libraries into generated JAR"

    And it exports everything (with other dependancies)

    and see that: Difference between extracting and packaging libraries into a jar file

    it helps ?