Hadoop Basics的MapReduce程序中的java.lang.NoClassDefFoundError

我正在尝试Hadoop的Basic MapReduce程序,其教程在http://java.dzone.com/articles/hadoop-basics-creating

该类的完整代码是(代码存在于上面的url上)

import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; public class Dictionary { public static class WordMapper extends Mapper { private Text word = new Text(); public void map(Text key, Text value, Context context) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString(), ","); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(key, word); } } } public static class AllTranslationsReducer extends Reducer { private Text result = new Text(); public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { String translations = ""; for (Text val : values) { translations += "|" + val.toString(); } result.set(translations); context.write(key, result); } } public static void main(String[] args) throws Exception { System.out.println("welcome to Java 1"); Configuration conf = new Configuration(); System.out.println("welcome to Java 2"); Job job = new Job(conf, "dictionary"); job.setJarByClass(Dictionary.class); job.setMapperClass(WordMapper.class); job.setReducerClass(AllTranslationsReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); job.setInputFormatClass(KeyValueTextInputFormat.class); FileInputFormat.addInputPath(job, new Path("/tmp/hadoop-cscarioni/dfs/name/file")); FileOutputFormat.setOutputPath(job, new Path("output")); System.exit(job.waitForCompletion(true) ? 0 : 1); } } 

但是在日食之后; 我收到了错误,

 welcome to Java 1 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.apache.hadoop.conf.Configuration.(Configuration.java:73) at Dictionary.main(Dictionary.java:43) Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 2 more 

当一个类在运行时不可见但在编译时可见时,会出现NoClassDefFoundError。 这可能与JAR文件有关,因为未包含所有必需的类文件。

所以尝试在类路径中添加commons-logging-1.1.1 jar,你可以从http://commons.apache.org/logging/download_logging.cgi中获取

请注意,exception是NoClassDefFoundError而不是ClassNotFoundException。

注意 :当类在运行时不可见但在编译时可见时,抛出NoClassDefFoundError。 这可能是在JAR文件的分发或生成中可能发生的事情,其中​​不包括所有必需的类文件。

修复 :请检查构建时间和运行时类路径的差异。

NoClassDefFoundError和ClassNotFoundException是不同的。 一个是错误,另一个是exception。

NoClassDefFoundError :来自JVM,在找到预期找到的类时遇到问题。 由于找不到类文件,在编译时工作的程序无法运行。

ClassNotFoundException :此exception表示在类路径上找不到类,即我们正在尝试加载类定义,并且包含该类的类/ jar在类路径中不存在。

当命名类成功定位在类路径中时,会发生NoClassDefFoundError,但由于某种原因无法加载和validation。 最常见的问题是validation命名类所需的另一个类要么丢失,要么是错误的版本。

一般来说,此错误意味着“仔细检查您的类路径中是否包含所有正确的JAR文件(正确版本)”。

在本地IDE(Eclipse)中运行Hadoop Map / Reduce程序时,这是一个非常常见的错误。

您应该已经在构建路径中添加了hadoop-core.jar,因此在您的程序中没有检测到编译错误。 但是当你运行它时会出现错误,因为hadoop-core依赖于commons-logging.jar(以及其他一些jar)。 您可能需要将/ lib下的jar添加到构建路径中。

我建议您使用Maven或其他依赖管理工具来管理依赖项。

请阅读文章: http : //kishorer.in/2014/10/22/running-a-wordcount-mapreduce-example-in-hadoop-2-4-1-single-node-cluster-in-ubuntu-14 -04-64位/ 。 它解释了如何在没有Marven的情况下引用Eclipse中的依赖项。 但是,根据我的理解,Marven是首选方式。