"main" java.lang.ClassNotFoundException怎么解决?

问题描述:

本人在自学MapReduce编程入门中,编写dailyAccessCount文件时运行:

编译生成dailyAccessCount.jar文件,提示有错误,我选择了yes

上传到Hadoop集群服务器节点,如图所示:

在Hadoop节点下执行命令时出现如标题所示异常,这是怎么回事?

dailyAccessCount代码如下:是代码出错了吗?还是什么原因?

import java.io.IOException;

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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class dailyAccessCount {

	public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> {

		private final static IntWritable one = new IntWritable(1);

		public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
			String line = value.toString();
			String array[] = line.split(","); // 指定空格为分隔符,组成数组
			String keyOutput = array[1]; // 提取数组中的访问日期做为Key
			context.write(new Text(keyOutput), one); // 组成键值对
		}
	}

	public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
		private IntWritable result = new IntWritable();

		public void reduce(Text key, Iterable<IntWritable> values, Context context)
				throws IOException, InterruptedException {
			int sum = 0; // 定义累加器,初始值为0
			for (IntWritable val : values) {
				sum += val.get(); // 将相同键的所有值进行累加
			}
			result.set(sum);
			context.write(key, result);
		}
	}

	public static void main(String[] args) throws Exception {

		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
		if (otherArgs.length < 2) {
			System.err.println("Usage: wordcount <in> [<in>...] <out>");
			System.exit(2);
		}
		Job job = new Job(conf, "Daily Access Count");
		job.setJarByClass(dailyAccessCount.class);
		job.setMapperClass(MyMapper.class);
		job.setReducerClass(MyReducer.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		for (int i = 0; i < otherArgs.length - 1; ++i) {
			FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
		}
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}

eclipse编译环境我也调成和书上一样的了:jdk1.7

鼓捣了一上午,还没解决。。

我解决了,是在eclipse中打包生成jar包时出错了,没有勾选主类的问题。 

你确定有这个文件吗,建议你试试不带txt后缀启动。

hadoop jar dailyAccessCount.jar  /user/root/user_login /user/root/AccessCount