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 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

View File

@ -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);
}