62,614
社区成员
发帖
与我相关
我的任务
分享
package com.dotest;
import java.net.URL;
import java.net.URLClassLoader;
public class Test {
private URLClassLoader loader;
public void doTest() {
resetLoader();
try {
Class testClass = Class.forName("com.dotest.test.ScopeTest", true,
loader);
TestIntf test = (TestIntf) testClass.newInstance();
test.doTest();
System.out.println("finished");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void resetLoader() {
loader = null;
try {
System.out.println("url: "
+ this.getClass().getResource("/testA.jar"));
loader = new URLClassLoader(new URL[] { this.getClass()
.getResource("/testA.jar") });
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Test test = new Test();
test.doTest();
}
}
package com.dotest;
public interface TestIntf {
public void doTest();
}
package com.dotest.test;
import com.dotest.TestIntf;
public class ScopeTest implements TestIntf {
@Override
public void doTest() {
System.out.println("doTest 111");
}
}
package com.dotest;
import java.net.URL;
import java.net.URLClassLoader;
public class Test {
private URLClassLoader loader;
public void doTest() {
resetLoader();
try {
Class testIntfClass = Class.forName("com.dotest.TestIntf", true,
loader);
System.out.println("111: " + testIntfClass.getName());
Class testClass = Class.forName("com.dotest.test.ScopeTest", true,
loader);
System.out.println("222: " + testClass.getName());
System.out.println("333: " + testClass.getInterfaces().length);
TestIntf test = (TestIntf) testClass.newInstance();
test.doTest();
System.out.println("finished");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void resetLoader() {
loader = null;
try {
System.out.println("url: "
+ this.getClass().getResource("/testA.jar"));
loader = new URLClassLoader(new URL[] { this.getClass()
.getResource("/testA.jar") });
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Test test = new Test();
test.doTest();
}
}
输出:
url: file:/C:/Users/linh/workspace/DynamicClassLoaderTest/bin/testA.jar
111: com.dotest.TestIntf
222: com.dotest.test.ScopeTest
333: 0
Exception in thread "main" java.lang.ClassCastException: com.dotest.test.ScopeTest cannot be cast to com.dotest.TestIntf
证明loader是可以load到interface和class的,可是不知道为什么出错了。
public class Test {
private URLClassLoader loader;
public void doTest() {
try {
Class testClass = Class.forName("com.ScopeTest");
TestIntf test = (TestIntf) testClass.newInstance();
test.doTest();
} catch (Exception e) {
e.printStackTrace();
}
}
public void resetLoader() {
loader = null;
try {
System.out.println("url: "
+ this.getClass().getResource("/testA.jar"));
loader = new URLClassLoader(new URL[] { this.getClass().getResource("/testA.jar") });
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
Test tt = new Test();
tt.doTest();
}
}