Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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