TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
自己开发了一个Classloader,继承自URLClassloader,希望对大家有所帮助 它可以做一般Classloader所必须做的事情,包括
(1)根据Url加载类
(2)如果本地的用file:///协议来获得的URL里面拿字节码的时候,过滤掉file:///
(3)获取字节码
(4)根据类名查找类文件
(5)载入类使用代码:- import java.net.*;
- import java.lang.reflect.Method;
- /**
- * @author charles.wang
- *
- */
- public class MainTest {
-
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- URL[] url = new URL[]{new URL("file:d:")};
- //添加想要加载的类路径,网络或者本地均可
- CustomClassLoader csl = new CustomClassLoader(url);
- csl.setExtensionName("class");
- Class cl = csl.loadClass("com.Demo");
- Object obj = cl.newInstance();
- Method method = cl.getMethod("printText", null);
- method.invoke(obj, null);
- }
-
- }
复制代码 |
|