Created
February 1, 2016 03:21
-
-
Save dangnhdev/4a4be08a573aac0f7102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.hs.acm.client.message.in.AcmClientReqMsg; | |
import com.hs.acm.client.message.in.JoinCallReqMsg; | |
import java.net.URL; | |
import java.net.URLClassLoader; | |
public class Test { | |
public static void main(String[] args) throws Exception { | |
URL[] urls = new URL[]{new URL("file:./lib/ACM_0.1.0.jar"), | |
new URL("file:./lib/ActiveStudy.Utility.2.0.1.jar")}; | |
/* | |
create an URLClassLoader, which only load class from /lib/ACM_0.1.0.jar and | |
lib/ActiveStudy.Utility.2.0.1.jar | |
I set its parent null because I just only want to load class in two jars provided | |
*/ | |
URLClassLoader clsLoader = URLClassLoader.newInstance(urls, null); | |
/*set it as context class loader for current thread */ | |
Thread.currentThread().setContextClassLoader(clsLoader); | |
/*load an test class */ | |
Class<?> clazz = clsLoader.loadClass("com.hs.acm.client.message.in.JoinCallReqMsg"); | |
/*create new instance*/ | |
Object msgObj = clazz.newInstance(); | |
/* | |
Problem arise here. If I create an URLClassLoader with null parent as above, | |
this line of code fire an exception (below). If I don't explicit set it, no problem at all | |
*/ | |
JoinCallReqMsg obj = (JoinCallReqMsg) msgObj; | |
System.out.println(obj instanceof JoinCallReqMsg); | |
System.out.println(obj instanceof AcmClientReqMsg); | |
System.out.println(obj.getClass().getSimpleName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment