我有一个依赖于某些依赖库的Java代理。我们有一个自定义的类加载器,它不进行globing,并提供确定性的类加载,并通过-Djava.system.class.loader=
指定。但是,似乎仍通过AppClassLoader加载java-agent类,从而导致某些ClassNotFoundException
。在浪费了几个小时之后,我遇到了这个JDK bug,它概述了这个确切的问题,现在已经关闭,但是仅针对JDK 9+进行了修复。
我想知道Java <= 8中是否有一种方法可以强制Java-agent类由指定的类加载器加载。
自定义类加载器看起来像这样,
public CustomClassloader(ClassLoader parent) throws Exception { //getJarPaths has the path of the java-agent jar as well as //all dependency jar it needs. super(getJarPaths(), parent); } private static URL[] getJarPaths() throws Exception { //custom implementation } public void appendToClassPathForInstrumentation(String path) throws MalformedURLException { assert(Thread.holdsLock(this)); super.addURL(Paths.get(path).toUri().toURL()); }
上面的类加载器与JDK9 +完美结合,而javaagent实际上是由自定义类加载器加载的。
我有一个依赖于某些依赖库的Java代理。我们有一个自定义的类加载器,它不会进行globing,而是提供确定性的类加载,并通过-Djava.system ....