Created
March 7, 2011 02:28
-
-
Save rednaxelafx/857977 to your computer and use it in GitHub Desktop.
show how the same Class file loaded by two classloaders become "incompatible"
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 java.io.*; | |
import java.net.*; | |
public class F { | |
static F instance; | |
public static void main(String[] args) throws Exception { | |
ClassLoader l = new URLClassLoader(new URL[] { new File(".").toURI().toURL()}, null); | |
Class<?> clazz = l.loadClass("F"); | |
F.class.getDeclaredField("instance").set(null, clazz.newInstance()); | |
} | |
} |
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
D:\code>java F | |
Exception in thread "main" java.lang.IllegalArgumentException: Can not set static F field F.instance to F | |
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) | |
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) | |
at sun.reflect.UnsafeStaticObjectFieldAccessorImpl.set(Unknown Source) | |
at java.lang.reflect.Field.set(Unknown Source) | |
at F.main(F.java:10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment