如何解决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.class
,FileCopyWithProgress$1.class
和FileCopyWithProgress.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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。