java - 尝试解压缩 7-Zip 存档(二进制模式)时出现异常

标签 java eclipse-rcp classpath 7zip eclipse-classpath

我正在 Eclipse 中运行 Java 应用程序。在这里,我尝试使用以下代码解压缩 7-Zip 存档文件:

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();

在 Debug模式下执行上述行集(在 Eclipse 中运行)时,我能够解压缩 7-zip 文件(没有问题)。

但是当尝试在二进制模式下执行相同操作时(将 Java 应用程序导出到 .JAR 文件后),我遇到了以下异常跟踪:

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

我还在 Java 应用程序的 .classpath 中添加了以下两行

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

但仍然没有解决问题。

任何帮助将不胜感激!!

最佳答案

不需要手动编写

尝试导出可运行的 Jar 文件/使用选项“将所需的库打包到生成的 JAR 中”

并且它导出所有内容(以及其他依赖项)

看到:Difference between extracting and packaging libraries into a jar file

有帮助吗?

关于java - 尝试解压缩 7-Zip 存档(二进制模式)时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33951404/

相关文章:

java - 光标没有移到第一个?

java - 可以将异常存储在变量中以便稍后抛出吗?

java - Spring Webflux : Controller return Mono<ResponseEntity<MyPojo>> vs Mono<MyPojo>

java - 使用 Java Web 启动的 Eclipse RCP 应用程序未启动

eclipse - Eclipse 平台中的循环依赖关系

java - 安装 Eclipse RCP 插件

java - 类冲突 : two jar files with the same classes

java 8 如何获得多个属性的不同列表

bash - 为什么 "No such file or directory"出现在我的环境变量中?

scala - 将多个类路径条目添加到 Scala REPL 类路径