Callback mechanism Java dynamically calls the jar Method

Source: Internet
Author: User

Some special projects require different functions in different environments. You can also upload a new JAR file to implement simple and direct hot deployment and run it in a new thread of the system. This ensures that the system does not need to be modified.Source code.

 

Package COM. shine. framework. jarutil; </P> <p> Import java.net. URL; <br/> Import java.net. urlclassloader; </P> <p> Import COM. shine. framework. core. util. reflectionutil; </P> <p>/** <br/> * jar utilities <br/> * @ author viruscodecn@gmail.com <br/> * @ project javaframework 1.0 <br/> */<br/> public class jarutil extends urlclassloader {<br/> Public jarutil (URL) {<br/> super (new URL [] {URL }); <br/>}</P> <p>/** <br/> * execute a method of class in jar <br/> * @ Param jarpath <br/> * @ Param classpath <br/> * @ Param methodname <br/> * @ Param ARGs <br/> * @ return <br/> */<br/> Public static object executejarclass (string jarpath, string classpath, <br/> string methodname, object... ARGs) {<br/> try {<br/> Object object = jarutil. getclassobject (jarpath, classpath); <br/> return reflectionutil. invokemethod (object, methodname, argS); <br/>} catch (exception ex) {<br/> ex. printstacktrace (); <br/>}< br/> return NULL; <br/>}</P> <p>/** <br/> * execute the main method of a class of jar <br/> * @ Param jarpath <br/> * @ Param classpath <br/> * @ Param ARGs <br/> */<br/> Public static void executejavaclassmainmethod (string jarpath, <br/> string classpath, object... ARGs) {<br/> try {<br/> Object object = jarutil. getclassobject (jarpath, classpath); <br/> reflectionutil. invokemainmethod (object, argS); <br/>} catch (exception ex) {<br/> ex. printstacktrace (); <br/>}</P> <p>/** <br/> * obtain a class in jar <br/> * @ Param jarpath <br/> * @ Param classpath <br/> * @ return <br/> */<br/> Public static object getclassobject (string jarpath, string classpath) {<br/> try {<br/> url = new URL (jarpath); <br/> jarutil T = new jarutil (URL ); </P> <p> Class C = T. findclass (classpath); <br/> return C. newinstance (); <br/>}catch (exception ex) {<br/> ex. printstacktrace (); <br/>}< br/> return NULL; <br/>}< br/>

 

Example

Package COM. shine. framework. jarutil; </P> <p> public class example {<br/> Public static void main (string ARGs []) {<br/> jarutil.exe cutejarclass ("test. jar "," test. test "," test "," 123 "); <br/>}< br/>

 

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.