Java编译不会生成.jar

时间:2014-11-03 22:12:47

标签: java hadoop jar compilation

我创建了简单的WordCount.java'用于实现简单的hadoop程序的文件,在编译时,它不会创建.jar文件。在WordCount.classWordCount$Map.classWordCount$Reduce.class创建的文件。我查看了WordCount.java文件,它确实包含了一个public static void main(String[] args)例程,所以它应该创建一个.jar文件,对吗?

这是我在很长一段时间内第一次尝试Java,所以它很容易成为Java编译的错误,但鉴于以下代码,在正确编译时它不应该给我一个.jar文件吗? / p>

package org.myorg;

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class WordCount {

  public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
  private final static IntWritable one = new IntWritable(1);
  private Text word = new Text();

  public void map(LongWritable key, Text value, Context context) throws IOException,         
             InterruptedException {
     String line = value.toString();
     StringTokenizer tokenizer = new StringTokenizer(line);
     while (tokenizer.hasMoreTokens()) {
         word.set(tokenizer.nextToken());
         context.write(word, one);
     }
  }
}

 public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {

 public void reduce(Text key, Iterator<IntWritable> values, Context context)
         throws IOException, InterruptedException {
     int sum = 0;
     while (values.hasNext()) {
         sum += values.next().get();
     }
     context.write(key, new IntWritable(sum));
  }
}

public static void main(String[] args) throws Exception {
  Configuration conf = new Configuration();
  Job job = new Job(conf, "wordcount");

  job.setOutputKeyClass(Text.class);
  job.setOutputValueClass(IntWritable.class);

  job.setMapperClass(Map.class);
  job.setReducerClass(Reduce.class);

  job.setInputFormatClass(TextInputFormat.class);
  job.setOutputFormatClass(TextOutputFormat.class);

  FileInputFormat.addInputPath(job, new Path(args[0]));
  FileOutputFormat.setOutputPath(job, new Path(args[1]));

  job.waitForCompletion(true);
  }

}

1 个答案:

答案 0 :(得分:3)

  

我创建了简单的WordCount.java&#39;用于实现简单的hadoop程序的文件,在编译时,它不会创建.jar文件。

不,它不会。 .java文件(包含javac)的汇编输出是.class个文件的集合。

然后使用jar工具创建一个包含这些类文件的jar文件以及您需要的任何其他资源。