Java dynamically loads a specified class or JAR package reflection calls its method

Source: Internet
Author: User
Tags instance method reflection
Preface

Sometimes, the project will use Java dynamic loading of the specified class or JAR package reflection to call its methods to achieve the separation of modules, so that the coupling between the various functions greatly reduced, more modular, more efficient code utilization. The proxy mode in the pattern uses the Java mechanism. Let's go through the code to see how to implement this feature. Code Detail

Package loadjarclass;
Import Java.io.File;
Import Java.lang.reflect.Method;
Import Java.net.URL;

Import Java.net.URLClassLoader;


Import Org.junit.Test; public class Loadjarclasstest {@Test the public void Testloadclass () throws exception{/* Dynamically loading the specified class/file File=new file
		("D:/test");//classpath (upper layer of package file) URL Url=file.touri (). Tourl (); ClassLoader loader=new urlclassloader (New Url[]{url});//Create Class loader//import
		Com.sun.org.apache.bcel.internal.util.ClassLoader; ClassLoader ClassLoader = new ClassLoader (New string[]{""});//Classpath class<?> cls=loader.loadclass ("Loadjarclass . Testtest ");//load the specified class, be sure to take the package name Object obj=cls.newinstance () of the class, and/or initialize an instance method Method=cls.getmethod (" Printstring ",
		String.class,string.class)/method name and corresponding parameter type Object o=method.invoke (obj, "Chen", "leixing");//Call method of the top System.out.println (string.valueof (o));//Output "chenleixing"/* Dynamically load the method of calling one of the classes by the specified jar package * * file=new file ("d:/test/
		Commons-lang3.jar ");//jar the path of the package Url=file.touri (). Tourl (); Loader=new UrlclassLoader (New Url[]{url});//Create Class loader Cls=loader.loadclass ("org.apache.commons.lang3.StringUtils");//load the specified class, note that you must bring the package name of the class Method=cls.getmethod ("center", string.class,int.class,string.class);//Method name and the type of the corresponding arguments O=method.invoke (null, "Chen" , Integer.valueof (10), "0"); the method (static method, the first argument that can be null) System.out.println (string.valueof (o)) is invoked.
 000chen000 "," Chen "string on both sides with 3" 0 "string}}


If you are interested in Stringsutils tool class friends, you can refer to my blog: StringUtils method of the complete introduction, Commons-lang common methods

Reflection and agent technical knowledge, can refer to my blog: Static proxy java, dynamic agent, Cglib dynamic agent, using dynamic agent based on AOP ASPECTJ framework-comparison and detailed, Java Reflection mechanism in-depth research

Java dynamically loads a specified class or JAR package reflection calls its method-involves classes in other jars analysis and solution of classnotfound problem

Related Article

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.