Java动态执行jar包中类的方法

动态加载执行jar包,在实际开发中经常会需要用到,尤其涉及平台和业务的关系的时候,业务逻辑部分可以独立出去交给业务方管理,业务方只需要提供jar包,就能在平台上运行。

结论

通过反射可以实现动态调用jar包中的类的方法

环境

jdk8

Springboot 2.7.14

测试环境介绍

新建一个SpringBoot项目,提供Controller的两个访问接口;m1接口调用jar包中的TestClass的m1方法;m2接口调用jar包中的TestClass的m2方法;

TestJar的包名:test.jar

TestJar下的测试类:TestClass

方法1:m1

方法2:m2(带参数)

测试步骤

  1. 将TestJar打成jar包

  2. 将TestJar-1.0-SNAPSHOT.jar放在待测试的SpringBoot项目的resource/lib下

  3. 启动SpringBoot项目,访问接口m1、m2

关键代码

TestController.java

java 复制代码
package cn.only.hww.test.controller;

import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;

/**
 * author       : Wayne Hu
 * date created : Created in 2024/7/8 11:32
 * description  :
 * class name   : TestController
 */
@RestController
@RequestMapping("test")
public class TestController {
    // 从resources目录加载jar文件
    ClassPathResource resource = new ClassPathResource("lib/TestJar-1.0-SNAPSHOT.jar");
    File jarFile = new File(System.getProperty("java.io.tmpdir") + "/TestJar-1.0-SNAPSHOT.jar");



    @GetMapping("m1")
    public String test1() {
        String result = null;
        try {

            // 创建URLClassLoader加载jar包
            URL jarUrl = jarFile.toURI().toURL();
            URLClassLoader classLoader = new URLClassLoader(new URL[]{jarUrl}, TestController.class.getClassLoader());

            // 加载类并调用方法
            Class<?> testClass = classLoader.loadClass("test.jar.TestClass");
            Object testClassInstance = testClass.getDeclaredConstructor().newInstance();
            FileCopyUtils.copy(resource.getInputStream(), Files.newOutputStream(jarFile.toPath()));

            Method m1Method = testClass.getMethod("m1");
            result = (String) m1Method.invoke(testClassInstance);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "test1 result: " + result;
    }

    @GetMapping("m2")
    public String test2() {
        String result = null;
        try {
            FileCopyUtils.copy(resource.getInputStream(), Files.newOutputStream(jarFile.toPath()));

            // 创建URLClassLoader加载jar包
            URL jarUrl = jarFile.toURI().toURL();
            URLClassLoader classLoader = new URLClassLoader(new URL[]{jarUrl}, TestController.class.getClassLoader());

            // 加载类并调用方法
            Class<?> testClass = classLoader.loadClass("test.jar.TestClass");
            Object testClassInstance = testClass.getDeclaredConstructor().newInstance();
            Method m1Method = testClass.getMethod("m2", String.class);
            String param = "{'user':'admin'}";
            result = (String) m1Method.invoke(testClassInstance, param);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "test1 result: " + result;
    }
}

TestClass.java

java 复制代码
package test.jar;

/**
 * author       : Wayne Hu
 * date created : Created in 2024/7/8 11:37
 * description  :
 * class name   : TestClass
 */
public class TestClass {
    public String m1(){
        System.out.println("执行jar包方法1");
        return "我是来自jar包的m1方法的返回数据";
    }

    public String m2(String data){
        System.out.println("执行jar包方法2,参数为:" + data);
        return "我是来自jar包的m2方法的返回数据,参数为:" + data;
    }
}
相关推荐
☆璇28 分钟前
【数据结构】排序
c语言·开发语言·数据结构·算法·排序算法
我要成为c嘎嘎大王28 分钟前
【C++】初识C++(1)
开发语言·c++
良木林32 分钟前
JavaScript书写基础和基本数据类型
开发语言·前端·javascript
梁同学与Android1 小时前
Android ---【内存优化】常见的内存泄露以及解决方案
android·java·内存泄漏
武子康3 小时前
Java-71 深入浅出 RPC Dubbo 上手 父工程配置编写 附详细POM与代码
java·分布式·程序人生·spring·微服务·rpc·dubbo
艾莉丝努力练剑3 小时前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
武子康5 小时前
Java-72 深入浅出 RPC Dubbo 上手 生产者模块详解
java·spring boot·分布式·后端·rpc·dubbo·nio
_殊途5 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
椰椰椰耶6 小时前
【Spring】拦截器详解
java·后端·spring
没有bug.的程序员7 小时前
JAVA面试宝典 - 《MyBatis 进阶:插件开发与二级缓存》
java·面试·mybatis