动态调用jar包方法

package cn.springBoot.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



import cn.springBoot.entity.User;
import cn.springBoot.service.UserService;
import cn.springBoot.util.FileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

/**
* <p>
* 前端控制器
* </p>
*
* @author ljx
* @since 2020-01-16
*/
@RestController
@RequestMapping("/user")
@Api(value="用户controller",tags={"用户操作接口"})
public class UserController {

public static void main(String[] args) {
// 系统类库路径
File libPath = new File("D:/jar");

// 获取所有的.jar和.zip文件
File[] jarFiles = libPath.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jar") || name.endsWith(".zip");
}
});

if (jarFiles != null) {
// 从URLClassLoader类中获取类所在文件夹的方法
// 对于jar文件,可以理解为一个存放class文件的文件夹
Method method = null;
try {
method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
boolean accessible = method.isAccessible(); // 获取方法的访问权限
try {
if (accessible == false) {
method.setAccessible(true); // 设置方法的访问权限
}
// 获取系统类加载器
URLClassLoader classLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();;
for (File file : jarFiles) {
try {
URL url = file.toURI().toURL();
method.invoke(classLoader, url);
Class c = Class.forName("cn.springBoot.util.FileUploadUtil");
Method m = c.getMethod("getVersion",String.class);
Object s = m.invoke(c,"sss");
System.out.println("读取jar文件成功"+file.getName() + s.toString());
} catch (Exception e) {
System.out.println("读取jar文件失败");
}
}
} finally {
method.setAccessible(accessible);
}
}
}

static class MyClassLoader extends URLClassLoader {

public MyClassLoader(URL[] urls) {
super(urls);
}

public MyClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}

public void addJar(URL url) {
this.addURL(url);
}

}
//
// private final static Logger logger = LoggerFactory.getLogger(SchoolController.class);
//
// @Autowired
// private UserService userService;
//
//
// @GetMapping("/getAll")
// @ApiOperation(value="获取全部user接口",response=User.class,responseContainer="list")
// public List<User> findAll() {
//
// logger.info("getAll");
//
// return userService.selectList(null);
//
// }
// @GetMapping("/getById")
// @ApiOperation(value="根据id获取user",response=User.class)
// public User getById(@RequestParam int id) {
//
// logger.info("getAll");
//
// return userService.selectByIda(id);
//
// }
//
// @GetMapping("/update")
// @ApiOperation(value="修改user",response=User.class)
// public User update(User user) {
//
// logger.info("update");
//
// return userService.updateUser(user);
//
// }
//
//
// @GetMapping("/del")
// @ApiOperation(value="删除")
// public String deleteEmp(Integer id){
// logger.info("del");
//
// return userService.delUser(id);
// }
//
//
// @PostMapping(value = "/fileUpload")
// @ApiOperation(value="图片上传")
// public String fileUpload(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
//
//
// return FileUploadUtil.fileUpLoad(file);
// }
//

}

原文地址:https://www.cnblogs.com/laixin09/p/15417620.html