/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
while (true) {
File filpath=new File("");
String path = "file:"+filpath.getAbsoluteFile()+"\\src\\datas\\";
URL newurl=new URL(path);
URLClassLoader classLoader=new URLClassLoader(new URL[]{newurl});
Class<?> methtClass = classLoader.loadClass("datas.bug");
Object obj = methtClass.newInstance();
methtClass.getDeclaredMethod("name").invoke(obj);
System.out.println(classLoader);
Thread.sleep(3000);
}
}
注意:path:一定要加上“file:”,不然会报错,其中“datas.bug”是加载的class包和文件名:
package datas;
public class bug {
public void name() {
System.out.println("测试结果");
}
}
更多交流:
JAVA 高级社区