2014-11-03 93 views
0

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

这是我第一次在Java中进行风险投资,因此在Java编译过程中很容易出错,但是考虑到下面的代码,在正确编译时它不应该给我一个.jar文件吗?

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); 
    } 

} 
+0

可能重复的[如何使一个可执行的jar文件?](http://stackoverflow.com/questions/5258159/how-to-make-an-executable-jar-file) – khelwood 2014-11-03 22:14:53

回答

3

我创建了简单的“WordCount.java”文件执行一个简单的Hadoop程序,并在编译时,它不会创建一个.jar文件。

不,它不会。 .java文件的编辑输出(与javac)是.class文件的集合。

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

+0

他正在使用一个控制台也许。 – blackSmith 2014-11-04 06:07:07

+0

@blackSmith:我没有看到该评论的相关性。 – 2014-11-04 07:08:43

+0

如果他习惯于使用IDE,我猜想事情会变得更简单。例如:在eclipse中,编译是自动的,创建jar比查找.class文件要容易一些。我自己有点怀疑我以前的评论的相关性:) – blackSmith 2014-11-04 07:21:21