Refer to several articles
The method used in the class urlclassloader that loads the jar package is Addurl, but this method is protected type
Two different methods are used
One
Reference http://blog.csdn.net/ozwarld/article/details/7440937
Reflect to this class
Method add = URLClassLoader.class.getDeclaredMethod ("Addurl", new class[] {url.class});
Add.setaccessible (TRUE);
Then execute add load jar package and then reflect again to get the class that needs to be executed
URLClassLoader ClassLoader = (urlclassloader) classloader.getsystemclassloader ();
URL url = new File (jarpath). Touri (). Tourl ();
Add.invoke (ClassLoader, new object[] {URL});
Class<?> clazz = Class.forName (className);
Method method = Clazz.getdeclaredmethod ("xxxx");
Method.invoke (Clazz.newinstance ());
The first use is the following method, feel simple, but because no parentclassloader, resulting in a variety of bugs, finally, although the tuning, but still chose the above method (not sure if there are other problems)
Two
Reference to the Http://www.cnblogs.com/flyingzl/articles/3139028.html
The article first establishes the subclass so that you can execute the Addurl method.
Static Class Myclassloader extends URLClassLoader {public
myclassloader (url[] URLs) {
super (URLs);
Public Myclassloader (url[] URLs, ClassLoader parent) {
super (URLs, parent);
}
public void Addjar (url url) {
this.addurl (URL);
}
}
Load the jar and perform
url[] urls = new url[] {};
Myclassloader ClassLoader = new Myclassloader (Urls,classloader.getsystemclassloader ())
try {
Classloader.addjar (New File ("C:/hello.jar"). Touri (). Tourl ());
Class<?> clazz = Classloader.loadclass ("Pakcagename.classname");
Method method = Clazz.getdeclaredmethod ("Run");
Method.invoke (Clazz.newinstance ());
Classloader.close ();
} catch (Exception e) {
e.printstacktrace ();
}
Other
If it is a local jar, this is OK, on the HDFs, there will be a problem, the URL does not support the HDFS protocol
Url.seturlstreamhandlerfactory (New Fsurlstreamhandlerfactory ());
This will support the HDFS protocol, path can be transferred URL, of course, have to load the HDFs related configuration file, get filesystem instance
Path PATH = fs.getfilestatus (new path (Jarpath)). GetPath ();
URL url = Path.touri (). Tourl ();
Classloader.addjar (URL);
If you're running a normal jar, it's on HDFs.
But
If you are loading a jar package for the MapReduce program, you can only be a local path, you cannot use the HDFs path, and in the Setjarbyclass method, you need to find the jar package absolute path
----------------------------------------------
After using a period of time found that the process is non-stop, the need to load the jar package is likely to need to be modified, so that after loading a different version of the jar package, the program will have bugs, read the latest jar package, is unlikely to update the jar package to restart the program
So, also studied the next Classloarder, in the current line Cheng jar package more insurance, Thread end will be released to ensure that each load is the latest, and does not account for memory
(URLClassLoader) Classloader.getsystemclassloader ();
Change to current thread class loader
(URLClassLoader) Thread.CurrentThread (). Getcontextclassloader ();