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

原文: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()); 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值