解決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.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());
如果本文對您有幫助,點一下右下角的“推薦”
相關文章
- Hadoop下面WordCount執行詳解Hadoop
- steam確認錯誤執行此操作時出現問題怎麼解決
- Java中解決多執行緒資料安全問題Java執行緒
- Qt 之 執行Qt Creator出現cannot find -lGL的問題的兩種解決QT
- 解決eclipse中出現Resource is out of sync with the file system問題Eclipse
- 執行 shell 指令碼 \r 問題解決指令碼
- 執行eclipse.exe時,出現錯誤An error has occurred.See the logfile問題EclipseError
- 多執行緒的出現是要解決什麼問題的? 本質什麼?執行緒
- 解決app中多語音出現的衝突問題APP
- 解決 eclipse cdt 執行時控制檯亂碼解決Eclipse
- 用jbuilder執行jive出現的問題,求教!UI
- 求教執行EJB程式時出現的問題
- 解決Ubuntu配置nginx出現的問題UbuntuNginx
- 使用MapReduce執行WordCount案例
- 解決jenkins執行磁碟滿的問題Jenkins
- mysql執行truncate drop 時卡死問題解決MySql
- mac 下PyCharm執行報錯問題解決MacPyCharm
- 使用tcpdump解決應用不能執行問題TCP
- windows下使用idea maven配置spark執行環境、執行WordCount例子以及碰到的問題WindowsIdeaMavenSpark
- 解決eclipse編輯jsp、js檔案時,經常出現卡死的問題EclipseJS
- Eclipse中執行出現selection does not contain a main type錯誤EclipseAI
- 發現問題,解決問題
- myeclispe中向mysql中插入中文資料出現??問題解決辦法LispMySql
- 解決Java執行過程中拋簽名異常的問題Java
- scalatest在idea和maven中執行錯誤的問題解決IdeaMaven
- Oracle中SQL語句執行效率問題的查詢與解決OracleSQL
- 解決TOAD中執行計劃顯示報錯的問題
- 解決react useEffect中的內容被執行兩次的問題React
- 解決eclipse中egit中的cannot open git-upload-pack問題EclipseGit
- Faiss使用多執行緒出現的效能問題AI執行緒
- SimpleJdonFrameworkTest安裝完後執行,出現問題Framework
- 【已解決】執行Eclipse出錯:Failed toload the JNI shared libraryEclipseAI
- 解決執行儲存過程出現許可權不足問題(ORA-01031)儲存過程
- Java中多執行緒的概述、實現方式、執行緒控制、生命週期、多執行緒程式練習、安全問題的解決...Java執行緒
- 快速解決mongodb出現id重複問題MongoDB
- 解決mysql出現幾個l的問題MySql
- PHP出現Cannotmodifyheaderinformation問題的解決方法PHPHeaderORM
- 多執行緒的安全問題及解決方案執行緒