2013-06-11 30 views
0

我正在運行hadoop wordcount程序。但它給我的錯誤,如 「NoClassDefFoundError的」wordcount程序中的NoClassDefFoundError

命令運行:

hadoop -jar /home/user/Pradeep/sample.jar hdp_java.WordCount /user/hduser/ana.txt /user/hduser/prout 
    Exception in thread "main" java.lang.NoClassDefFoundError: WordCount 
    Caused by: java.lang.ClassNotFoundException: WordCount 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 
    Could not find the main class: WordCount. Program will exit. 

我已經在Eclipse中創建的程序,然後導出爲jar文件

的Eclipse代碼:

package hdp_java; 

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.LongWritable; 
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.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, Iterable<IntWritable> values, Context context) 
    throws IOException, InterruptedException { 
    int sum = 0; 
    for (IntWritable val : values) { 
     sum += val.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

用7zip之類的東西打開你的Jar,並檢查你的wordCount.class是否應該在哪裏。 – skoll

回答

2

你需要告訴Hadoop的工作要使用的罐子像這樣:

job.setJarByClass(WordCount.class); 

此外,一定要在提交一份工作就像在下面的例子中添加任何依賴於兩個HADOOP_CLASSPATH-libjars

使用以下方法來添加所有的jar依賴從(例如)當前和lib目錄:

export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:`echo *.jar`:`echo lib/*.jar | sed 's/ /:/g'` 

請記住,通過hadoop jar開始工作時,您還需要通過使用-libjars將任何依賴關係的罐子傳遞給它。我喜歡用:

hadoop jar <jar> <class> -libjars `echo ./lib/*.jar | sed 's/ /,/g'` [args...] 

注:sed命令需要不同的分隔符; HADOOP_CLASSPATH:分開,-libjars需要分開,

+0

嗨羽蛇神, 我添加setJarByClass主類清單屬性,但它仍然是給我的錯誤。但這一次是不同的。 和所有相關罐子已經在Eclipse 增加了失敗的你是如何讓你的罐子從 /home/user/hpcc/lz_data/Pradeep/sample.jar – prad

+0

加載主類清單屬性? – Quetzalcoatl

+0

我在eclipse中使用export-> jar文件選項(indigo) – prad

1

在代碼中加入這一行:

job.setJarByClass(WordCount.class); 

如果仍然不起作用這項工作導出爲一個罐子,把它添加到自己作爲一個外部罐子,看看它是否工作。

+0

嗨塔裏克, 添加上面的行後,它給了我不同的錯誤。 警告:$ HADOOP_HOME已棄用。 無法加載從 /home/user/hpcc/lz_data/Pradeep/sample.jar – prad

相關問題