![](/img/trans.png)
[英]Java How to load classes out of a jar in the classpath with the System ClassLoader (no URLClassLoader)?
[英]understanding urlclassloader, how to access a loaded jar's classes
我试图了解如何使用URLClassLoader访问/提供一个jar文件。
首先,我用
package myA;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import org.jgroups.JChannel;
public class loader {
JChannel channel;
String user_name=System.getProperty("user.name", "n/a");
private void start() throws Exception {
channel=new JChannel(); // use the default config, udp.xml
channel.connect("ChatCluster");
}
public void loadMe()throws ClassNotFoundException, MalformedURLException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
URL classUrl;
classUrl = new URL("file:///home/myJars/jgroups-3.4.2.Final.jar");
URL[] classUrls = { classUrl };
URLClassLoader ucl = new URLClassLoader(classUrls);
Class<?> c = ucl.loadClass("org.jgroups.JChannel");
for(Field f: c.getDeclaredFields()) {
System.out.println("Field name=" + f.getName());
}
Object instance = c.newInstance();
//Method theMethod = c.getMethod("main");
//theMethod.invoke(instance);
}
public static void main(String[] args) throws Exception {
new loader().loadMe();
new loader().start();
}
}
打印输出显示jgroups-3.4.2.Final.jar中已声明的字段,但是随后抛出classnotfound错误。
java -cp myA.jar myA.loader
Field name=DEFAULT_PROTOCOL_STACK
Field name=local_addr
Field name=address_generator
Field name=name
Field name=cluster_name
Field name=my_view
Field name=prot_stack
Field name=state_promise
Field name=state_transfer_supported
Field name=flush_supported
Field name=config
Field name=stats
Field name=sent_msgs
Field name=received_msgs
Field name=sent_bytes
Field name=received_bytes
Field name=probe_handler
Exception in thread "main" java.lang.NoClassDefFoundError: org/jgroups/JChannel
at myA.loader.start(loader.java:23)
at myA.loader.main(loader.java:45)
Caused by: java.lang.ClassNotFoundException: org.jgroups.JChannel
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 2 more
我不明白为什么打印输出显示已加载该类,但是却找不到该类?
艺术
您的代码可能有几个问题。 首先,您在main中实例化加载程序2次,因此第二个实例与第一个实例无关,并且可能不知道第一个实例加载了JChannel
的类文件定义。
此外,您之前已将JChannel定义为loader
的成员,因此JRE在启动时应为其定义类定义-否则它不应该知道该字段应该是什么。 我已将其替换为您通过URLClassLoader
加载的类,您应该在start()
实例化该类。
package myA;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import org.jgroups.JChannel;
public class Loader
{
Class<?> clazz;
String user_name=System.getProperty("user.name", "n/a");
private void start() throws Exception
{
if (this.clazz == null)
throw new Exception("Channel class was not loaded properly");
Object channel = this.clazz.newInstance(); // use the default config, udp.xml
Method chatCluster = this.clazz.getDeclaredMethod("connect", new Class[] { String.class });
chatCluster.invoke(channel, "ChatCluster");
}
public void loadMe() throws Exception
{
URL classUrl;
classUrl = new URL("file:///home/myJars/jgroups-3.4.2.Final.jar");
URL[] classUrls = { classUrl };
URLClassLoader ucl = new URLClassLoader(classUrls);
Class<?> c = ucl.loadClass("org.jgroups.JChannel");
for(Field f: c.getDeclaredFields())
{
System.out.println("Field name=" + f.getName());
}
this.clazz = c;
Object instance = c.newInstance();
//Method theMethod = c.getMethod("main");
//theMethod.invoke(instance);
}
public static void main(String[] args) throws Exception
{
Loader loader = new Loader();
loader.loadMe();
loader.start();
}
}
您应该进一步在代码中添加一些错误处理。
Exception in thread "main" java.lang.NoClassDefFoundError: org/jgroups/JChannel
at myA.loader.start(loader.java:23)
代码在此行失败:
channel=new JChannel(); // use the default config, udp.xml
加载程序的ClassLoader不可见JChannel类型。 如果您尝试以下操作,这将很明显:
loader.class
.getClassLoader()
.loadClass("org.jgroups.JChannel");
在运行时,您不应有对依赖项的任何编译时引用,而这些依赖关系将不在类型的类路径上。
使用新的子类ClassLoader进行加载不会将该类型添加到某些全局类池中。 加载程序具有子级与父级关系的层次结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.