Created
December 15, 2013 03:21
-
-
Save uphy/7968462 to your computer and use it in GitHub Desktop.
How to read objects which are loaded dynamically.
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
package dct; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.io.ObjectStreamClass; | |
import java.net.URL; | |
import java.net.URLClassLoader; | |
public class ClassLoaderAndObjectIOTest { | |
public static void main(String[] args) throws Throwable { | |
final File libFile = new File("lib.jar"); // lib.jar contains class "dct.Hoge" | |
final File objFile = new File("obj.bin"); | |
try { | |
ClassLoaderAndObjectIOTest.class.getClassLoader().loadClass("dct.Hoge"); | |
} catch (ClassNotFoundException ex) { | |
System.out.println("Current classloader doesn't know dct.Hoge."); | |
} | |
final URL url = libFile.toURI().toURL(); | |
final URLClassLoader classLoader = new URLClassLoader(new URL[] {url}); | |
try { | |
ClassLoaderAndObjectIOTest.class.getClassLoader().loadClass("dct.Hoge"); | |
} catch (ClassNotFoundException ex) { | |
System.out.println("> Current classloader doesn't know dct.Hoge."); | |
} | |
System.out.println("Export external library object."); | |
final Object obj = classLoader.loadClass("dct.Hoge").newInstance(); | |
final ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(objFile)); | |
oos.writeObject(obj); | |
oos.close(); | |
System.out.println("Load the object without specifying classloader."); | |
try { | |
load(objFile); | |
} catch (Throwable e) { | |
System.out.println("> Can't load because classloader doesn't know dct.Hoge."); | |
} | |
Thread t = new Thread() { | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void run() { | |
try { | |
load(objFile); | |
} catch (Throwable e) { | |
System.out.println("> Can't load because context classloader isn't used for loading."); | |
} | |
} | |
}; | |
t.setContextClassLoader(classLoader); | |
System.out.println("Load with setting context classloader."); | |
t.start(); | |
t.join(); | |
System.out.println("Load with specific classloader."); | |
try { | |
loadWithClassLoader(objFile, classLoader); | |
System.out.println("> Can load because loaded with the classloader know the class."); | |
} catch (Throwable e) { | |
e.printStackTrace(); | |
} | |
} | |
private static Object load(File file) throws ClassNotFoundException, IOException { | |
return loadWithClassLoader(file, null); | |
} | |
private static Object loadWithClassLoader(File file, final ClassLoader classLoader) throws ClassNotFoundException, IOException { | |
ObjectInputStream ois; | |
if (classLoader == null) { | |
ois = new ObjectInputStream(new FileInputStream(file)); | |
} else { | |
ois = new ObjectInputStream(new FileInputStream(file)) { | |
@Override | |
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { | |
final String name = desc.getName(); | |
return classLoader.loadClass(name); | |
} | |
}; | |
} | |
final Object loadedObj = ois.readObject(); | |
ois.close(); | |
return loadedObj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment