有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hadoop抱怨不存在匿名类(NoClassDefFoundError)

考虑一个简单的java文件,它创建一个^ {< CD1>},将本地文件^ {< CD2>}复制到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

我编译代码并使用

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

但是,我收到了错误

Exception in thread "main" java.lang.NoClassDefFoundError: FileCopyWithProgress$1

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


共 (1) 个答案

  1. # 1 楼答案

    我发现了这个问题,所以我只是在发帖子,以防它对某人有所帮助。我必须把类FileCopyWithProgress$1.class包含在JAR中。正确的答案应该是

    jar cf FileCopyWithProgress.jar FileCopyWithProgress*.class