【0】问题描述
0.1)晚辈我想利用URLClassLoader 加载某目录下的class文件,执行结果发现,Eclipse执行正确,而命令行却抛出异常ClassNotFoundException,求各位前辈帮帮忙。。谢谢。
package com.tomcat.classloader;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
public class URLClassLoaderTest {
public final static String dir = System.getProperty("user.dir") +
File.separator + "webroot" + File.separator;
public static void main(String[] args) {
URL u;
try {
File file = new File(dir);
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};
URLClassLoader loader = new URLClassLoader(urls);
Class<?> beanClass = loader.loadClass("servlet.Bean");
System.out.println("hello, class loader.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Bean {
public void sayHello() {
System.out.println("Hello from loaded Bean class for servlet package!!!");
}
public static void main(String[] args) {
System.out.println("i am a bean!");
}
}