首页 > 解决方案 > 使用 Apache Commons 时遇到错误“java.lang.NoClassDefFoundError”

问题描述

使用 Java 程序在 Ubuntu 20.04 LTS 中使用 OpenJDK 1.6 测试 Apache commons(commons.compress 和 commons.io)。构建正常,但遇到错误(如下)。apache.commons.compress 和 apache.commons.io 都与程序位于同一目录中。

构建程序的命令如下: javac unTar.java --module-path 。--类路径。--add-modules org.apache.commons.io,org.apache.commons.compress

import java.io.*;
import org.apache.commons.compress.archivers.tar.*;
//import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.io.IOUtils;

public class unTar {  
        public static void main(String[] args) throws Exception{
                /* To read individual TAR file */
                TarArchiveEntry entry = null;
                String individualFiles;
                int offset;
                FileOutputStream outputFile=null;
                TarArchiveInputStream myTarFile=  new TarArchiveInputStream(new FileInputStream(new File("tar_ball.tar")));
                /* Create a loop to read every single entry in TAR file */
                while ((entry = myTarFile.getNextTarEntry()) != null) {
                        /* Get the name of the file */
                        individualFiles = entry.getName();
                        /* Get Size of the file and create a byte array for the size */
                        byte[] content = new byte[(int) entry.getSize()];
                        offset=0;
                        /* Some SOP statements to check progress */
                        System.out.println("File Name in TAR File is: " + individualFiles);
                        System.out.println("Size of the File is: " + entry.getSize());
                        System.out.println("Byte Array length: " + content.length);
                        /* Read file from the archive into byte array */
                        myTarFile.read(content, offset, content.length - offset);
                        /* Define OutputStream for writing the file */
                        outputFile=new FileOutputStream(new File(individualFiles));
                        /* Use IOUtiles to write content of byte array to physical file */
                        IOUtils.write(content,outputFile);
                        /* Close Output Stream */
                        outputFile.close();
                }
                /* Close TarAchiveInputStream */
                myTarFile.close();
        }
}


Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/tar/TarArchiveInputStream
    at unTar.main(unTar.java:14)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.compress.archivers.tar.TarArchiveInputStream
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)


    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
    ... 1 more

标签: javatarapache-commons

解决方案


java -cp commons-io-VERSION.jar:commons-compress-VERSION.jar unTar

以上是向 java 运行时提供所需类路径的常规方法。不确定这是否是您的问题。


推荐阅读