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;
    }
}
相关推荐
巨龙之路1 小时前
C语言中的assert
c语言·开发语言
2301_776681652 小时前
【用「概率思维」重新理解生活】
开发语言·人工智能·自然语言处理
熊大如如3 小时前
Java 反射
java·开发语言
猿来入此小猿3 小时前
基于SSM实现的健身房系统功能实现十六
java·毕业设计·ssm·毕业源码·免费学习·猿来入此·健身平台
ll7788113 小时前
C++学习之路,从0到精通的征途:继承
开发语言·数据结构·c++·学习·算法
我不想当小卡拉米3 小时前
【Linux】操作系统入门:冯诺依曼体系结构
linux·开发语言·网络·c++
teacher伟大光荣且正确3 小时前
Qt Creator 配置 Android 编译环境
android·开发语言·qt
炎芯随笔3 小时前
【C++】【设计模式】生产者-消费者模型
开发语言·c++·设计模式
goTsHgo3 小时前
Spring Boot 自动装配原理详解
java·spring boot
卑微的Coder4 小时前
JMeter同步定时器 模拟多用户并发访问场景
java·jmeter·压力测试