日期:2014-05-20 浏览次数:20817 次
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");
}
}
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();
}
}