Urlclassloader usage and case programs

Source: Internet
Author: User

References: http://blog.csdn.net/shixin1198/article/details/3733948

1. Usage Overview:

File file = new file (full JAR file path );
URL url = file. tourl ();
Urlclassloader loader = new urlclassloader (new URL [] {URL });
Class tidyclazz = loader. loadclass (full name of the class containing the package name );
2. Detailed description:

We know that Java uses classloader to load classes into the memory, and in the same application, there can be many classloader, through the delegate mechanism, the load task is passed to the upper-level loader, and so on until the class loader is started (there is no parent class loader ). If the class loader can load this class, it will first load it. If not, pass it down. When the parent class is null, the built-in JVM class (called Bootstrap Class Loader) will act as the parent class. Think about the increasing use of XML files for configuration files, descriptors, and deployment operators. In fact, the configuration information described in the XML document will eventually become a Java class, and the base is completed through classloader. Urlclassloader is a subclass of classloader, which is used
The search path of the JAR file and directory URL to load classes and resources. In other words, you can use urlclassloader to load the class in the specified jar to the memory.
The following is an example. In this example, we need to use urlclassloader to load the jar and run a method of the class.
(1) create a Java project: Urlclassloadertest0 Define an interface so that all classes that inherit it must implement the Action method, as follows:
<textarea readonly name="code" class="java">Package urlclassloadertest0; </P> <p> // defines the interface actioninterface <br/> Public interface actioninterface {<br/> Public String action (); <br/>}</textarea> Package it into the testinterface. jar file.
(2) create a Java project: Urlclassloadertest1 Introduce the testinterface. jar package. Create the testaction class to implement the actioninterface interface. As follows:
<textarea readonly name="code" class="java">Package urlclassloadertest1; </P> <p> Import urlclassloadertest0.actioninterface; <br/> // import testinterface to the project. jar package to implement the interface actioninterface. the jar package name cannot be a class name <br/> public class testaction implements actioninterface {<br/> Public String action () {<br/> return "This actiontest class "; <br/>}< br/>}</textarea> Package it as test. jar and put it in the root directory of drive D. The following is how to use urlclassloader to load and run the Action Method of testaction, and print the returned value on the console.
(3) create a Java project: Urlclassloadertest2 To introduce the testinterface. jar package. Create an executable class (main method) and add the following to it: Code :
Package urlclassloadertest2; </P> <p> Import java.net. *; <br/> Import Java. io. *; <br/> Import urlclassloadertest0.actioninterface; // import the testinterface package to the project </P> <p> public class classloadertest {<br/> Public static void main (string ARGs []) {<br/> try {<br/> file = new file ("rtest.txt"); <br/> bufferedreader in = new bufferedreader (New filereader (File )); <br/> string S = new string (); <br/> while (S = in. R Eadline ())! = NULL) {<br/> // URL read from rtest.txt, create a class loader according to the URL <br/> URL url = new URL (s ); <br/> S = NULL; <br/> urlclassloader myclassloader = new urlclassloader (<br/> new URL [] {URL}, thread. currentthread (). getcontextclassloader (); <br/> system. out. println (myclassloader); <br/> class myclass = myclassloader. loadclass ("urlclassloadertest1.testaction"); <br/> actioninterface action = (actioninterface) myclass. newinstance (); <br/> string STR = action. action (); <br/> system. out. println (STR); </P> <p> // create a Class Loader Based on url1 <br/> URL url1 = new URL ("file: D:/test. jar "); <br/> urlclassloader myclassloader1 = new urlclassloader (<br/> new URL [] {url1}, thread. currentthread (). getcontextclassloader (); <br/> class myclass1 = myclassloader1.loadclass ("urlclassloadertest1.testaction"); <br/> actioninterface Action1 = (actioninterface) myclass1.newinstance (); <br/> string str1 = action1.action (); <br/> system. out. println (str1); </P> <p >}< br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p>}This code first creates a file under the project Rtext.txt To store the path of the jar to be loaded. (Path: file: D:/test. Jar) , Read a path and load

Running result:

Java.net. urlclassloader @ 152b6651 <br/> This actiontest class <br/>Note:

The jar package name in the preceding package cannot be the same as the class name, for example, testaction. jar and actioninterface. jar.

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.