Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use reflection mechanism to call jar in Java

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use reflection mechanism to call jar in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use reflection mechanism to call jar in Java.

The following shows the method of calling the image.Buddy class from the image.jar package.

Public class Main {public static void main (String [] args) {try {/ / both ways can be URL url = new File ("/ Users/wuchen/Documents/IntelliJIDEA/Applet/out/production/Applet/image.jar"). ToURI (). ToURL (); / / URL url = new URL ("file:/Users/wuchen/Documents/IntelliJIDEA/Applet/out/production/Applet/image.jar") URL [] urls = new URL [] {url}; URLClassLoader loader = new URLClassLoader (urls); Class c = loader.loadClass ("image.Buddy"); Object o = c.newInstance (); Method m = c.getMethod ("sleep"); m.invoke (o);} catch (NoSuchMethodException e) {e.printStackTrace () } catch (IllegalAccessException e) {e.printStackTrace ();} catch (InstantiationException e) {e.printStackTrace ();} catch (ClassNotFoundException e) {e.printStackTrace ();} catch (InvocationTargetException e) {e.printStackTrace ();} catch (MalformedURLException e) {e.printStackTrace () } there are several points to pay attention to:

URL is "file:/Users/wuchen/Documents/IntelliJIDEA/Applet/out/production/Applet/image.jar"

There is no slash behind the image.jar

Second:

The name of the class is image.Buddy, so don't omit the image in image.Buddy just because there is image in URL.

Third:

Buddy.class should be placed in the image folder, and then package the image folder instead of packaging all the files in the image folder, and then change the package name to image.jar

Add: java dynamically loads the specified class or jar package reflection calls its method

Preface

Occasionally. In the project, java will be used to dynamically load the specified class or jar package reflection to call its methods to achieve the separation of modules, so that the coupling between various functions is greatly reduced and more modular. Higher code utilization. The proxy pattern in the pattern uses this mechanism of java.

Let's look at how to implement this function through the code.

The specific code 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 public void testLoadClass () throws Exception {/ * dynamically loads the specified class * / File file=new File ("D:/test"); / / the classpath (one layer above the package file) URL url=file.toURI (). ToURL (); ClassLoader loader=new URLClassLoader (new URL [] {url}) / / create a classloader / / 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 include the package name of the class Object obj=cls.newInstance (); / / 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 the above method method System.out.println (String.valueOf (o)) / / output "chenleixing" / * dynamically load the method of a class in the specified jar package call * / file=new File ("D:/test/commons-lang3.jar"); / / the path of the jar 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, be sure to include the package name of the class method=cls.getMethod ("center", String.class,int.class,String.class); / / the method name and the corresponding parameter type o=method.invoke (null, "chen", Integer.valueOf (10), "0"); / / call the above method method (static method, the first parameter can be null) System.out.println (String.valueOf (o)) / output "000chen000", "chen" string with 3 "0" strings on each side}} Thank you for reading, this is the content of "how to use reflection mechanism to call jar in Java". After the study of this article, I believe you have a deeper understanding of how to use reflection mechanism to call jar in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report