java加载url类,Java如何使用System ClassLoader(没有URLClassLoader)从类路径中的jar中加载类?...

So, I need to load some class at runtime with the System ClassLoader out of a jar in the classpath, but every time I try, I get a ClassNotFoundException. With the System ClassLoader, am I able to do just: x.y.classineed (x and y being packages) or would I have to do something like: pathtox.x.y.classineed, assuming it's even possible to do this?

解决方案

The JAR must not be in your CLASSPATH.

This works fine: I have the JDOM JAR in my CLASSPATH.

package cruft;

/**

* ClassLoaderDemo

* @author Michael

* @since 2/9/12 7:09 PM

* @link http://stackoverflow.com/questions/9220887/java-how-to-load-classes-out-of-a-jar-in-the-classpath-with-the-system-classload

*/

public class ClassLoaderDemo {

public static void main(String[] args) {

try {

ClassLoader classLoader = ClassLoaderDemo.class.getClassLoader();

if (classLoader != null) {

Class clazz = classLoader.loadClass("org.jdom.Document");

System.out.println(clazz.getName());

}

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值