在运行时加载.jars:我的加载器工作,但使用setContextClassLoader不行?

时间:2021-10-22 19:43:55

I have an URLClassLoader named "MyClassLoader" set up with some jars on it. If I try

我有一个名为“MyClassLoader”的URLClassLoader设置了一些罐子。如果我试试

MyClassLoader.loadClass("MyClass");

it works.

If I try

如果我试试

Thread.currentThread().setContextClassLoader(MyClassLoader);
Thread.currentThread().getContextClassLoader().loadClass("MyClass");

it also works.

它也有效。

But If I try

但是,如果我尝试

Thread.currentThread().setContextClassLoader(MyClassLoader);
Class.forName("MyClass");

it throws a ClassNotFoundException.

它抛出一个ClassNotFoundException。

Of course, Class.forName here is just an example; trying to use MyClass throws the exception just as well.

当然,这里的Class.forName只是一个例子;尝试使用MyClass也会抛出异常。

All this probably means I don't understand how setContextClassLoader works. Could anyone clarify this to me and help me understand it - and what should I do to make my code work? Thank you.

所有这些可能意味着我不明白setContextClassLoader是如何工作的。任何人都可以向我澄清这一点并帮助我理解它 - 我该怎么做才能使我的代码工作?谢谢。

1 个解决方案

#1


public static Class forName(String className) throws ClassNotFoundException

public static Class forName(String className)抛出ClassNotFoundException

Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:

返回与具有给定字符串名称的类或接口关联的Class对象。调用此方法相当于:

Class.forName(className, true, currentLoader) where currentLoader denotes the defining class loader of the current class.

Class.forName(className,true,currentLoader)其中currentLoader表示当前类的定义类加载器。

Try:

Class.forName("MyClass", true, MyClassLoader);

[Class.forName(String, boolean, ClassLoader][1]

[Class.forName(String,boolean,ClassLoader] [1]

The likely problem you are encountering is that you are trying to forName a Class that you loaded with a custom ClassLoader. However, you are using the form of forName that implicitly uses the ClassLoader which loaded the Class making the call. In most cases, this will be the system ClassLoader.

您遇到的可能问题是您正在尝试forName使用自定义ClassLoader加载的类。但是,您使用的是forName的形式,它隐式使用加载了进行调用的Class的ClassLoader。在大多数情况下,这将是系统ClassLoader。

ClassLoaders can get complex.

ClassLoaders可能变得复杂。

[1]: http://java.sun.com/javase/6/docs/api/java/lang/Class.html#forName(java.lang.String, boolean, java.lang.ClassLoader)

[1]:http://java.sun.com/javase/6/docs/api/java/lang/Class.html#forName(java.lang.String,boolean,java.lang.ClassLoader)

#1


public static Class forName(String className) throws ClassNotFoundException

public static Class forName(String className)抛出ClassNotFoundException

Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:

返回与具有给定字符串名称的类或接口关联的Class对象。调用此方法相当于:

Class.forName(className, true, currentLoader) where currentLoader denotes the defining class loader of the current class.

Class.forName(className,true,currentLoader)其中currentLoader表示当前类的定义类加载器。

Try:

Class.forName("MyClass", true, MyClassLoader);

[Class.forName(String, boolean, ClassLoader][1]

[Class.forName(String,boolean,ClassLoader] [1]

The likely problem you are encountering is that you are trying to forName a Class that you loaded with a custom ClassLoader. However, you are using the form of forName that implicitly uses the ClassLoader which loaded the Class making the call. In most cases, this will be the system ClassLoader.

您遇到的可能问题是您正在尝试forName使用自定义ClassLoader加载的类。但是,您使用的是forName的形式,它隐式使用加载了进行调用的Class的ClassLoader。在大多数情况下,这将是系统ClassLoader。

ClassLoaders can get complex.

ClassLoaders可能变得复杂。

[1]: http://java.sun.com/javase/6/docs/api/java/lang/Class.html#forName(java.lang.String, boolean, java.lang.ClassLoader)

[1]:http://java.sun.com/javase/6/docs/api/java/lang/Class.html#forName(java.lang.String,boolean,java.lang.ClassLoader)