HADOOP编译WordCount.java文件报错

问题现象

使用javac编译hadoop例子wordcount.java出现报错如下。

[root@node4 hadoop]# javac -classpath ${HADOOP_HOME}/hadoop-${HADOOP_VERSION}-core.jar -d wordcount_classes WordCount.java
WordCount.java:6: error: package org.apache.hadoop.mapreduce does not exist
import org.apache.hadoop.mapreduce.*;
^
WordCount.java:7: error: package org.apache.hadoop.conf does not exist
import org.apache.hadoop.conf.*;
^
WordCount.java:8: error: package org.apache.hadoop.io does not exist
import org.apache.hadoop.io.*;
^
WordCount.java:9: error: package org.apache.hadoop.util does not exist
import org.apache.hadoop.util.*;
^
WordCount.java:10: error: package org.apache.hadoop.fs does not exist
import org.apache.hadoop.fs.Path;
                           ^
WordCount.java:25: error: cannot find symbol
       extends Mapper{
               ^
  symbol:   class Mapper
  location: class WordCount
WordCount.java:25: error: cannot find symbol
       extends Mapper{
                              ^
  symbol:   class Text
  location: class WordCount
WordCount.java:25: error: cannot find symbol
       extends Mapper{
                                    ^
  symbol:   class Text
  location: class WordCount
WordCount.java:25: error: cannot find symbol
       extends Mapper{
                                          ^
  symbol:   class IntWritable
  location: class WordCount
WordCount.java:27: error: cannot find symbol
    private final static IntWritable one = new IntWritable(1);
                         ^
  symbol:   class IntWritable
  location: class TokenizerMapper
WordCount.java:28: error: cannot find symbol
    private Text word = new Text();
            ^
  symbol:   class Text
  location: class TokenizerMapper
WordCount.java:30: error: cannot find symbol
    public void map(Object key, Text value, Context context
                                ^
  symbol:   class Text
  location: class TokenizerMapper
WordCount.java:30: error: cannot find symbol
    public void map(Object key, Text value, Context context
                                            ^
  symbol:   class Context
  location: class TokenizerMapper
WordCount.java:41: error: cannot find symbol
       extends Reducer {
               ^
  symbol:   class Reducer
  location: class WordCount
WordCount.java:41: error: cannot find symbol
       extends Reducer {
                       ^
  symbol:   class Text
  location: class WordCount
WordCount.java:41: error: cannot find symbol
       extends Reducer {
                            ^
  symbol:   class IntWritable
  location: class WordCount
WordCount.java:41: error: cannot find symbol
       extends Reducer {
                                        ^
  symbol:   class Text
  location: class WordCount
WordCount.java:41: error: cannot find symbol
       extends Reducer {
                                             ^
  symbol:   class IntWritable
  location: class WordCount
WordCount.java:42: error: cannot find symbol
    private IntWritable result = new IntWritable();
            ^
  symbol:   class IntWritable
  location: class IntSumReducer
WordCount.java:44: error: cannot find symbol
    public void reduce(Text key, Iterable values,
                       ^
  symbol:   class Text
  location: class IntSumReducer
WordCount.java:44: error: cannot find symbol
    public void reduce(Text key, Iterable values,
                                          ^
  symbol:   class IntWritable
  location: class IntSumReducer
WordCount.java:45: error: cannot find symbol
                       Context context
                       ^
  symbol:   class Context
  location: class IntSumReducer
WordCount.java:27: error: cannot find symbol
    private final static IntWritable one = new IntWritable(1);
                                               ^
  symbol:   class IntWritable
  location: class TokenizerMapper
WordCount.java:28: error: cannot find symbol
    private Text word = new Text();
                            ^
  symbol:   class Text
  location: class TokenizerMapper
WordCount.java:42: error: cannot find symbol
    private IntWritable result = new IntWritable();
                                     ^
  symbol:   class IntWritable
  location: class IntSumReducer
WordCount.java:48: error: cannot find symbol
      for (IntWritable val : values) {
           ^
  symbol:   class IntWritable
  location: class IntSumReducer
WordCount.java:57: error: cannot find symbol
    Configuration conf = new Configuration();
    ^
  symbol:   class Configuration
  location: class WordCount
WordCount.java:57: error: cannot find symbol
    Configuration conf = new Configuration();
                             ^

原因

import与hadoop有关的类,失败。是因为/etc/profile里面关于hadoop的classpath定义不正确。

解决方法

编辑/etc/profile然后source /etc/profile
增加如下代码

export CLASSPATH=.:$HADOOP_HOME/share/hadoop/common/hadoop-common-2.7.4.jar:$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.7.4.jar:$HADOOP_HOME/share/hadoop/common/lib/commons-cli-1.2.jar:$CLASSPATH

验证

[root@node4 hadoop]# javac -d wordcount_classes WordCount.java
[root@node4 hadoop]# 

OK了。


javac 是java语言编程编译器,全称javacompilation。
javac工具读由java语言编写的类和接口的定义,并将它们编译成字节代码的class文件。

你可能感兴趣的:(踩过的坑)