Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Created March 7, 2011 02:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rednaxelafx/857977 to your computer and use it in GitHub Desktop.
Save rednaxelafx/857977 to your computer and use it in GitHub Desktop.
show how the same Class file loaded by two classloaders become "incompatible"
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());
}
}
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