View code
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 }
Add the following to the mian method:
File jarfile = ejob. createtempjar ("Bin");
Ejob. addclasspath ("/Usr/hadoop/conf");
Classloader = ejob. getclassloader ();
Thread. currentthread (). setcontextclassloader (classloader );
...
(Jobconf) job. getconfiguration (). setjar (jarfile. tostring ());