Exception in thread "main" java.lang.ClassNotFo...

1.如果wordcount程序不含层次,即没有package

那么使用如下命令:

[cpp] view plain copy print ?
  1. hadoop jar wordcount.jar WordCount2 /home/hadoop/input/20418.txt /home/hadoop/output/wordcount2-6  
hadoop jar wordcount.jar WordCount2 /home/hadoop/input/20418.txt /home/hadoop/output/wordcount2-6

该命令行的意思大致是:执行hadoop 程序,该程序在wordcount.jar中。该wordcount.jar包含以下几个class文件,分别是WordCount.java编译产生的3个class文件:

  • WordCount.class
  • WordCount$Map.class
  • WordCount$Reduce.class

和WordCount2.java编译产生的四个claas文件:

  •  classWordCount2.class
  • WordCount2$IntSumReducer.class
  • WordCount2$IntWritableDecreasingComparator.class 
  • WordCount2$TokenizerMapper.class

并且这几个.class文件是在jar包的根目录下。将上述七个class文件打包成jar文件的方法如下:假设上述七个class文件在同一个目录WordCount文件夹下,使用命令行进入到该级目录,然后通过如下命令打包:

[java] view plain copy print ?
  1. jar cvf WordCount.jar *.class  
jar cvf WordCount.jar *.class

2.如果wordcount程序含有层次

那么使用如下命令(错误)

[cpp] view plain copy print ?
  1. $ hadoop jar wordcount.jar WordCount2 /home/hadoop/input/20418.txt /home/hadoop/output/wordcount2-7  
$ hadoop jar wordcount.jar WordCount2 /home/hadoop/input/20418.txt /home/hadoop/output/wordcount2-7
会报错,错误如下:(具体原因可以参考博客: http://blog.csdn.net/xw13106209/article/details/6861855)
[cpp] view plain copy print ?
  1. Exception in thread "main" java.lang.ClassNotFoundException: WordCount2  
  2.     at java.net.URLClassLoader$1.run(URLClassLoader.java:202)  
  3.     at java.security.AccessController.doPrivileged(Native Method)  
  4.     at java.net.URLClassLoader.findClass(URLClassLoader.java:190)  
  5.     at java.lang.ClassLoader.loadClass(ClassLoader.java:306)  
  6.     at java.lang.ClassLoader.loadClass(ClassLoader.java:247)  
  7.     at java.lang.Class.forName0(Native Method)  
  8.     at java.lang.Class.forName(Class.java:247)  
  9.     at org.apache.hadoop.util.RunJar.main(RunJar.java:149)  
Exception in thread "main" java.lang.ClassNotFoundException: WordCount2
	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 java.lang.ClassLoader.loadClass(ClassLoader.java:247)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:247)
	at org.apache.hadoop.util.RunJar.main(RunJar.java:149)
正确命令如下:
[cpp] view plain copy print ?
  1. $ hadoop jar WordCount.jar org.apache.hadoop.examples.WordCount2 /home/hadoop/input/20418.txt /home/hadoop/output/wordcount2-7  
$ hadoop jar WordCount.jar org.apache.hadoop.examples.WordCount2 /home/hadoop/input/20418.txt /home/hadoop/output/wordcount2-7

这里唯一的不同色jar包的不同,上一条命令的jar包是wordcount.jar,而这里的java包是WordCount.jar。WordCount.jar把是对org/apache/hadoop/examples整个目录打包得到的。

你可能感兴趣的:(Exception in thread "main" java.lang.ClassNotFo...)