java - 使用 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
解决方案
java -cp commons-io-VERSION.jar:commons-compress-VERSION.jar unTar
以上是向 java 运行时提供所需类路径的常规方法。不确定这是否是您的问题。
推荐阅读
- ansible - 如何将 ansible 调试日志保存到单个文件
- node.js - 在heroku上部署mongodb atlas后无法解决错误?
- python - 我想将数据保存在 sqlite 数据库中,我的查询在网页中不起作用但相同的代码在 Django Shell 中运行良好
- javascript - 带有html 5模式的Angular中的正则表达式荷兰邮政编码
- apache-kafka - Kafka Streams 中跨密钥更新的消息顺序
- java - Java Quartz,每个作业只允许一个线程
- android - 如何制作一个通用的 LiveData 类来处理 SharedPreferences?
- django - 带有 Django 的 Angular 路由器保护
- javascript - 同一地图上的多个选择交互不起作用
- c - 将库中定义的函数名作为 C 中的函数指针传递