ClassLoaderUtil.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package com.plugin.users.utils;
  2. import lombok.SneakyThrows;
  3. import lombok.extern.slf4j.Slf4j;
  4. import java.lang.reflect.Method;
  5. import java.net.URL;
  6. import java.net.URLClassLoader;
  7. /**
  8. * 类加载器工具类
  9. *
  10. * @author zlt
  11. * @version 1.0
  12. * @date 2021/10/5
  13. * <p>
  14. * Blog: https://zlt2000.gitee.io
  15. * Github: https://github.com/zlt2000
  16. */
  17. @Slf4j
  18. public class ClassLoaderUtil {
  19. private static Method method = Loader_addURL_Method();
  20. @SneakyThrows
  21. private static Method Loader_addURL_Method() {
  22. Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
  23. if (!method.isAccessible()) {
  24. method.setAccessible(true);
  25. }
  26. return method;
  27. }
  28. public static ClassLoader getClassLoader(String url) {
  29. try {
  30. URLClassLoader classLoader = new URLClassLoader(new URL[]{}, ClassLoaderUtil.class.getClassLoader());
  31. method.invoke(classLoader, new URL(url));
  32. return classLoader;
  33. } catch (Exception e) {
  34. log.error("getClassLoader-error", e);
  35. return null;
  36. }
  37. }
  38. }