解决Eclipse中运行WordCount出现 java.lang.ClassNotFoundException: org.apache.hadoop.examples.WordCount$TokenizerMapper问题

简介: 原文:http://tonymomo.pixnet.net/blog/post/62329497 1 package org.apache.hadoop.examples; 2 3 import java.

原文:http://tonymomo.pixnet.net/blog/post/62329497

 1 package org.apache.hadoop.examples;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 import java.net.URL;
 8 import java.net.URLClassLoader;
 9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.jar.JarEntry;
12 import java.util.jar.JarOutputStream;
13 import java.util.jar.Manifest;
14 
15 public class EJob {
16 
17     // To declare global field
18     private static List<URL> classPath = new ArrayList<URL>();
19 
20     // To declare method
21     public static File createTempJar(String root) throws IOException {
22         if (!new File(root).exists()) {
23             return null;
24         }
25         Manifest manifest = new Manifest();
26         manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
27         final File jarFile = File.createTempFile("EJob-", ".jar", new File(
28                 System.getProperty("java.io.tmpdir")));
29 
30         Runtime.getRuntime().addShutdownHook(new Thread() {
31             public void run() {
32                 jarFile.delete();
33             }
34         });
35 
36         JarOutputStream out = new JarOutputStream(
37                 new FileOutputStream(jarFile), manifest);
38         createTempJarInner(out, new File(root), "");
39         out.flush();
40         out.close();
41         return jarFile;
42     }
43 
44     private static void createTempJarInner(JarOutputStream out, File f,
45             String base) throws IOException {
46         if (f.isDirectory()) {
47             File[] fl = f.listFiles();
48             if (base.length() > 0) {
49                 base = base + "/";
50             }
51             for (int i = 0; i < fl.length; i++) {
52                 createTempJarInner(out, fl[i], base + fl[i].getName());
53             }
54         } else {
55             out.putNextEntry(new JarEntry(base));
56             FileInputStream in = new FileInputStream(f);
57             byte[] buffer = new byte[1024];
58             int n = in.read(buffer);
59             while (n != -1) {
60                 out.write(buffer, 0, n);
61                 n = in.read(buffer);
62             }
63             in.close();
64         }
65     }
66 
67     public static ClassLoader getClassLoader() {
68         ClassLoader parent = Thread.currentThread().getContextClassLoader();
69         if (parent == null) {
70             parent = EJob.class.getClassLoader();
71         }
72         if (parent == null) {
73             parent = ClassLoader.getSystemClassLoader();
74         }
75         return new URLClassLoader(classPath.toArray(new URL[0]), parent);
76     }
77 
78     public static void addClasspath(String component) {
79 
80         if ((component != null) && (component.length() > 0)) {
81             try {
82                 File f = new File(component);
83 
84                 if (f.exists()) {
85                     URL key = f.getCanonicalFile().toURL();
86                     if (!classPath.contains(key)) {
87                         classPath.add(key);
88                     }
89                 }
90             } catch (IOException e) {
91             }
92         }
93     }
94 
95 }

mian方法中添加:

File jarFile = EJob.createTempJar("bin");

EJob.addClasspath("/usr/hadoop/conf");

ClassLoader classLoader = EJob.getClassLoader();

Thread.currentThread().setContextClassLoader(classLoader);

。。。

((JobConf) job.getConfiguration()).setJar(jarFile.toString()); 

如果本文对您有帮助,点一下右下角的“推荐”
目录
相关文章
|
29天前
|
存储 分布式计算 Hadoop
基于Java的Hadoop文件处理系统:高效分布式数据解析与存储
本文介绍了如何借鉴Hadoop的设计思想,使用Java实现其核心功能MapReduce,解决海量数据处理问题。通过类比图书馆管理系统,详细解释了Hadoop的两大组件:HDFS(分布式文件系统)和MapReduce(分布式计算模型)。具体实现了单词统计任务,并扩展支持CSV和JSON格式的数据解析。为了提升性能,引入了Combiner减少中间数据传输,以及自定义Partitioner解决数据倾斜问题。最后总结了Hadoop在大数据处理中的重要性,鼓励Java开发者学习Hadoop以拓展技术边界。
47 7
|
3月前
|
Java Android开发
Eclipse 运行程序
Eclipse 运行程序
51 2
|
3月前
|
Java Android开发
Eclipse Java 构建路径
Eclipse Java 构建路径
54 3
|
3月前
|
Java Android开发
Eclipse 创建 Java 项目
Eclipse 创建 Java 项目
63 4
|
3月前
|
Java Android开发
Eclipse 运行配置(Run Configuration)
Eclipse 运行配置(Run Configuration)
109 1
|
3月前
|
Java Android开发
Eclipse 创建 Java 接口
Eclipse 创建 Java 接口
48 1
|
3月前
|
Java Android开发
Eclipse 创建 Java 包
Eclipse 创建 Java 包
46 1
|
3月前
|
数据库连接 PHP Apache
PHP在Apache中如何运行?
PHP在Apache中如何运行?
78 5
|
4月前
|
分布式计算 大数据 Java
大数据-86 Spark 集群 WordCount 用 Scala & Java 调用Spark 编译并打包上传运行 梦开始的地方
大数据-86 Spark 集群 WordCount 用 Scala & Java 调用Spark 编译并打包上传运行 梦开始的地方
69 1
大数据-86 Spark 集群 WordCount 用 Scala & Java 调用Spark 编译并打包上传运行 梦开始的地方
|
4月前
|
IDE Java 编译器
Java:如何确定编译和运行时类路径是否一致
类路径(Classpath)是JVM用于查找类文件的路径列表,对编译和运行Java程序至关重要。编译时通过`javac -classpath`指定,运行时通过`java -classpath`指定。IDE如Eclipse和IntelliJ IDEA也提供界面管理类路径。确保编译和运行时类路径一致,特别是外部库和项目内部类的路径设置。
323 5

热门文章

最新文章

推荐镜像

更多