Hadoop抱怨不存在匿名类NoClassDefFoundError

问题描述

考虑一个简单的Java文件,该文件创建一个BufferedInputStream来将本地文件1400-8.txt复制到Hadoop HDFS并打印一些点作为进度状态。该示例是Hadoop书here中的示例3-3。

// cc FilecopyWithProgress copies a local file to a Hadoop filesystem,and shows progress
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IoUtils;
import org.apache.hadoop.util.Progressable;

// vv FilecopyWithProgress
public class FilecopyWithProgress {
  public static void main(String[] args) throws Exception {
    String localSrc = args[0];
    String dst = args[1];
    
    InputStream in = new BufferedInputStream(new FileInputStream(localSrc));
    
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(dst),conf);
    OutputStream out = fs.create(new Path(dst),new Progressable() {
      public void progress() {
        System.out.print(".");
      }
    });
    
    IoUtils.copyBytes(in,out,4096,true);
  }
}
// ^^ FilecopyWithProgress

我编译代码并使用创建一个JAR文件

hadoop com.sun.tools.javac.Main FilecopyWithProgress.java
jar cf FilecopyWithProgress.jar FilecopyWithProgress.class

以上命令生成文件FilecopyWithProgress.classFilecopyWithProgress$1.classFilecopyWithProgress.jar。然后,我尝试运行它

hadoop jar FilecopyWithProgress.jar FilecopyWithProgress 1400-8.txt hdfs://localhost:9000/user/kostas/1400-8.txt

但是,我收到了错误消息

线程“主”中的异常java.lang.NoClassDefFoundError: FilecopyWithProgress $ 1

据我了解,FilecopyWithProgress$1.class是由于程序声明的匿名回调函数引起的。但是既然文件存在,这里的问题是什么?我运行的命令顺序正确吗?

解决方法

我发现了问题,因此我只是在发布,以防它对某人有所帮助。我必须将类FileCopyWithProgress$1.class包含在JAR中。正确的应该是

jar cf FileCopyWithProgress.jar FileCopyWithProgress*.class