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

转载至:这里

在examples包中建立EJob类:

package org.apache.hadoop.examples;

 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.jar.JarEntry;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;

 public class EJob {

     // To declare global field
     private static List<URL> classPath = new ArrayList<URL>();

     // To declare method
     public static File createTempJar(String root) throws IOException {
         if (!new File(root).exists()) {
             return null;
         }
         Manifest manifest = new Manifest();
         manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
         final File jarFile = File.createTempFile("EJob-", ".jar", new File(
                 System.getProperty("java.io.tmpdir")));

         Runtime.getRuntime().addShutdownHook(new Thread() {
             public void run() {
                 jarFile.delete();
             }
         });

         JarOutputStream out = new JarOutputStream(
                 new FileOutputStream(jarFile), manifest);
         createTempJarInner(out, new File(root), "");
         out.flush();
         out.close();
         return jarFile;
     }

     private static void createTempJarInner(JarOutputStream out, File f,
             String base) throws IOException {
         if (f.isDirectory()) {
             File[] fl = f.listFiles();
             if (base.length() > 0) {
                 base = base + "/";
             }
             for (int i = 0; i < fl.length; i++) {
                 createTempJarInner(out, fl[i], base + fl[i].getName());
             }
         } else {
             out.putNextEntry(new JarEntry(base));
             FileInputStream in = new FileInputStream(f);
             byte[] buffer = new byte[1024];
             int n = in.read(buffer);
             while (n != -1) {
                 out.write(buffer, 0, n);
                 n = in.read(buffer);
             }
             in.close();
         }
     }

     public static ClassLoader getClassLoader() {
         ClassLoader parent = Thread.currentThread().getContextClassLoader();
         if (parent == null) {
             parent = EJob.class.getClassLoader();
         }
         if (parent == null) {
             parent = ClassLoader.getSystemClassLoader();
         }
         return new URLClassLoader(classPath.toArray(new URL[0]), parent);
     }

     public static void addClasspath(String component) {

         if ((component != null) && (component.length() > 0)) {
             try {
                 File f = new File(component);

                 if (f.exists()) {
                     URL key = f.getCanonicalFile().toURL();
                     if (!classPath.contains(key)) {
                         classPath.add(key);
                     }
                 }
             } catch (IOException e) {
             }
         }
     }

 }

接着在WordCount的Main方法中加入如下代码:

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

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

ClassLoader classLoader = EJob.getClassLoader();

Thread.currentThread().setContextClassLoader(classLoader);

。。。

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

再次运行Run on hadoop即可得到结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值