Java Hot update load class and Jar__java

Source: Internet
Author: User

1, first understand the basic principles of Java several loaders:

Class Loaders in Java can be roughly divided into two classes, one provided by the system and the other by Java application developers. The system provides a class loader that consists of three of the following:
Boot class loader (bootstrap class loader): It is used to load Java core libraries, which are implemented in native code and do not inherit from Java.lang.ClassLoader.
Extended class loader (Extensions class loader): It is used to load Java extension libraries. The implementation of the Java virtual machine provides an extension library directory. The class loader finds and loads Java classes in this directory.
System class Loader (loader): It loads Java classes based on the Java application's Classpath (CLASSPATH). In general, Java-applied classes are loaded by it. It can be obtained by Classloader.getsystemclassloader ().

In addition to the system-provided classloader, developers can implement their own classloader by inheriting the Java.lang.ClassLoader class to meet some special requirements.

The order in which we look for classes in the class loader when the program executes is: Boot class loader-"Extended class loader-" System class Loader-"We customize some ClassLoader, each class loader has its own space, the same loader inside the binary name of the class must be unique, Of course, the same class can also exist in different loader memory area, but we look for the class is in order, but the search will not continue to look down, and finally did not find the report class does not exist anomalies.
If we want to load classes dynamically, we'll imitate our servers like Tomcat and WebLogic, and their development model is to load all classes into their own classloader, and when they are replaced they reload the new class into memory. Thus the dynamic loading of the class is realized.

2, the use of the summary:

File File = new file (full path to jar file);
URL url = File.touri (). Tourl ();
URLClassLoader loader = new URLClassLoader (new url[] {URL});
Class Tidyclazz = Loader.loadclass (the full name of the desired class containing the package name);

Detailed Description:

We know that Java uses ClassLoader to load classes into memory, and in the same application, there can be many classloader, by delegating mechanism, the loaded task passed to the superior loader, and so on, and so on, until the Boot class loader (no superior class loader). If the Startup class loader can load this class, it will load first. If not, it is passed down. When the parent class is NULL, the JVM's built-in class (called the Bootstrap class loader) acts as the parent class. Think of the growing number of XML files being used as profiles or descriptors and deployment characters. In fact, these configuration information described through the XML document will eventually become Java class, in fact, through the ClassLoader to complete. URLClassLoader is a subclass of ClassLoader that is used to load classes and resources from the search path of the URL that points to the JAR file and directory. In other words, the class in the specified jar can be loaded into memory by URLClassLoader.

Reference code: public class Reloadclassloader extends classloader{
/**
*
* @param classfile class file
* @return
* @throws Exception
*/
Public class<?> loadclass (File classfile) {
General class files are usually less than 100k, and if the actual situation exceeds this range, you can enlarge the length
byte bytes[] = new byte[(int) classfile.length ()];
FileInputStream FIS = null;
Class<?> clazz = null;
Try
{
FIS = new FileInputStream (classfile);
int j = 0;
while (true)
{
int i = fis.read (bytes);
if (i = = 1)
Break
j = i;
}
Clazz = super.defineclass (null, Bytes, 0, J);
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
finally {
if (FIS!= null) {
try {
Fis.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
return clazz;
}
/**
*
* @param jarfile Jar Pack
* @param className Full class name
* @return
* @throws Exception
*/
Public class<?> Loadjar (File jarfile,string className) {
Class<?> CLS = null;
if (! Jarfile.exists ()) {
return null;
}

try {
URL url = Jarfile.touri (). Tourl ();
URLClassLoader ClassLoader = new URLClassLoader (new url[] {URL}, Thread.CurrentThread (). Getcontextclassloader ());
CLS = Classloader.loadclass (className);
catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return CLS;
}
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.