From 76ad13e7d06156001b7f2373a92d6a64bb040e16 Mon Sep 17 00:00:00 2001 From: CaptainB Date: Wed, 6 Apr 2022 18:25:50 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E5=85=BC=E5=AE=B9jdk=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E5=8A=A0=E8=BD=BDjar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- .../io/metersphere/service/PluginService.java | 29 +++++++------------ 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6c6b243db5..394daef4f3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ ENV JAVA_CLASSPATH=/opt:/opt/lib/ms-jmeter-core.jar:/opt/lib/* ENV JAVA_MAIN_CLASS=io.metersphere.Application ENV AB_OFF=true ENV MS_VERSION=${MS_VERSION} -ENV JAVA_OPTIONS="-Dfile.encoding=utf-8 -Djava.awt.headless=true" +ENV JAVA_OPTIONS="-Dfile.encoding=utf-8 -Djava.awt.headless=true --add-opens java.base/jdk.internal.loader=ALL-UNNAMED" RUN mv /opt/lib/ms-jmeter-core*.jar /opt/lib/ms-jmeter-core.jar diff --git a/backend/src/main/java/io/metersphere/service/PluginService.java b/backend/src/main/java/io/metersphere/service/PluginService.java index 396467f3c1..d5b209527f 100644 --- a/backend/src/main/java/io/metersphere/service/PluginService.java +++ b/backend/src/main/java/io/metersphere/service/PluginService.java @@ -24,7 +24,6 @@ import javax.annotation.Resource; import java.io.File; import java.lang.reflect.Method; import java.net.URL; -import java.net.URLClassLoader; import java.util.*; import java.util.stream.Collectors; @@ -110,24 +109,18 @@ public class PluginService { } private void loadJar(String jarPath) { - File jarFile = new File(jarPath); - // 从URLClassLoader类中获取类所在文件夹的方法,jar也可以认为是一个文件夹 - Method method = null; try { - method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); - } catch (NoSuchMethodException | SecurityException e1) { - e1.printStackTrace(); - } - // 获取方法的访问权限以便写回 - try { - method.setAccessible(true); - // 获取系统类加载器 - URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); - - URL url = jarFile.toURI().toURL(); - //URLClassLoader classLoader = new URLClassLoader(new URL[]{url}); - - method.invoke(classLoader, url); + ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + try { + Method method = classLoader.getClass().getDeclaredMethod("addURL", URL.class); + method.setAccessible(true); + method.invoke(classLoader, new File(jarPath).toURI().toURL()); + } catch (NoSuchMethodException e) { + Method method = classLoader.getClass() + .getDeclaredMethod("appendToClassPathForInstrumentation", String.class); + method.setAccessible(true); + method.invoke(classLoader, jarPath); + } } catch (Exception e) { LogUtil.error(e); }