java Hadoop 中的 NoClassDefFoundError 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16829054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:06:34  来源:igfitidea点击:

NoClassDefFoundError Error in Hadoop

javahadoopruntime-errornoclassdeffounderror

提问by SLearner

So after going through many posts on SO and revising some java basics, I still get this error

因此,在阅读了许多关于 SO 的帖子并修改了一些 Java 基础知识之后,我仍然收到此错误

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/io/Writable
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2451)
at java.lang.Class.getMethod0(Class.java:2694)
at java.lang.Class.getMethod(Class.java:1622)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)

I know the difference between java.lang.NoClassDefFoundErrorand ClassNotFoundExceptionin Java, and have looked through what usually causes it. In a nutshell, it is because some class is unavailable to the program during Run Time but is available during Compile time. Hence I get no compile time errors.

我知道Javajava.lang.NoClassDefFoundErrorClassNotFoundExceptionJava 中的区别,并且查看了通常导致它的原因。简而言之,这是因为某些类在运行时对程序不可用,但在编译时可用。因此我没有得到编译时错误。

I have added two classpaths, one to commons-logging-1.1.3.jarand the other to the hadoop-core.*jar. I am pretty confident that the classpaths are correct.

我已经添加了两个类路径,一个commons-logging-1.1.3.jar和其他的hadoop-core.*jar。我非常有信心类路径是正确的。

Here are the imports in my Program

这是我的程序中的导入

import java.io.*;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.SequenceFile.*;
import org.apache.hadoop.io.SequenceFile.Writer;
import org.apache.hadoop.io.*;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.*;

采纳答案by Tejas Patil

You need to create a jar out of the java code like given here:

您需要使用此处给出的 java 代码创建一个 jar :

$ mkdir my_classes 
$ javac -classpath $HADOOP_HOME/hadoop-$HADOOP_VERSION-core.jar -d my_classes <name of the main class> 
$ jar -cvf <name of the jar> -C my_classes .

Run the jar this way:

以这种方式运行 jar:

$HADOOP_HOME/bin/hadoop jar <name of the jar> <name of the main class> <arguments to the program>

See the documentation pageof hadoop for the description of jar command.

有关jar 命令的说明,请参阅hadoop的文档页面