首先确保hadoop已经正确安装、配置以及运行。
拷贝WordCount.java到我们的文件夹,下载的hadoop里带有WordCount.java,路径为:
hadoop-0.20.203.0/src/examples/org/apache/hadoop/examples/WordCount.java
进行拷贝操作
[hadoop@localhost~]$ cp hadoop-0.20.203.0/src/examples/org/apache/hadoop/examples/WordCount.java ~
[hadoop@localhost~]$ ls
Desktop hadoop-0.20.203.0 WordCount.java
在当前目录下创建一个用来存放WordCount.class的文件夹
[hadoop@localhost~]$ mkdir classes
编译WordCount.java
[hadoop@localhost~]$ javac -classpath hadoop-0.20.203.0/hadoop-core-0.20.203.0.jar -d classesWordCount.java
上面的方法是按照hadoop自带的doc文档进行编译的,如果发现报错,出现如下异常
WordCount.java:53:error: cannot access Options
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
^
class file for org.apache.commons.cli.Optionsnot found
1 error
则按如下进行编译
[hadoop@localhost~]$ javac -classpath hadoop-0.20.203.0/hadoop-core-0.20.203.0.jar:hadoop-0.20.203.0/lib/commons-cli-1.2.jar -d classes WordCount.java
编译成功,classes下会出现一个org的文件夹
[hadoop@localhost~]$ ls classes
org
对编译好的class进行打包
[hadoop@localhost~]$ jar -cvf wordcount.jar -C classes/ .
added manifest
adding: org/(in =0) (out= 0)(stored 0%)
adding:org/apache/(in = 0) (out= 0)(stored 0%)
adding:org/apache/hadoop/(in = 0) (out= 0)(stored 0%)
adding: org/apache/hadoop/examples/(in= 0) (out= 0)(stored 0%)
adding:org/apache/hado