build: 兼容jdk版本的加载jar

This commit is contained in:
CaptainB 2022-04-06 18:25:50 +08:00 committed by 刘瑞斌
parent 12a915cda1
commit 76ad13e7d0
2 changed files with 12 additions and 19 deletions

View File

@ -13,7 +13,7 @@ ENV JAVA_CLASSPATH=/opt:/opt/lib/ms-jmeter-core.jar:/opt/lib/*
ENV JAVA_MAIN_CLASS=io.metersphere.Application ENV JAVA_MAIN_CLASS=io.metersphere.Application
ENV AB_OFF=true ENV AB_OFF=true
ENV MS_VERSION=${MS_VERSION} 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 RUN mv /opt/lib/ms-jmeter-core*.jar /opt/lib/ms-jmeter-core.jar

View File

@ -24,7 +24,6 @@ import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -110,24 +109,18 @@ public class PluginService {
} }
private void loadJar(String jarPath) { private void loadJar(String jarPath) {
File jarFile = new File(jarPath);
// 从URLClassLoader类中获取类所在文件夹的方法jar也可以认为是一个文件夹
Method method = null;
try { try {
method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); ClassLoader classLoader = ClassLoader.getSystemClassLoader();
} catch (NoSuchMethodException | SecurityException e1) { try {
e1.printStackTrace(); Method method = classLoader.getClass().getDeclaredMethod("addURL", URL.class);
} method.setAccessible(true);
// 获取方法的访问权限以便写回 method.invoke(classLoader, new File(jarPath).toURI().toURL());
try { } catch (NoSuchMethodException e) {
method.setAccessible(true); Method method = classLoader.getClass()
// 获取系统类加载器 .getDeclaredMethod("appendToClassPathForInstrumentation", String.class);
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); method.setAccessible(true);
method.invoke(classLoader, jarPath);
URL url = jarFile.toURI().toURL(); }
//URLClassLoader classLoader = new URLClassLoader(new URL[]{url});
method.invoke(classLoader, url);
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e); LogUtil.error(e);
} }