Created
June 18, 2015 17:50
-
-
Save hinerm/5721562ade5562d8599b to your computer and use it in GitHub Desktop.
Test for URLClassLoader.close
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.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.net.URL; | |
import java.net.URLClassLoader; | |
public class Main { | |
// Usage: run this class. It will block, waiting for input. Enter any string ("end" to exit). | |
// Each iteration calls Test.printMe, which prints a message to standard out. | |
// If the calls to URLClassLoader.close are commented out, the original jar can not be | |
// modified or deleted. | |
// If the calls are uncommented, while the program is waiting for input you can change the | |
// output of Test.printMe and rebuild the jar. The program output will change accordingly | |
// on subsequent iterations. | |
public static void main(String... args) throws ClassNotFoundException, | |
IOException, NoSuchMethodException, SecurityException, | |
IllegalAccessException, IllegalArgumentException, | |
InvocationTargetException { | |
// FIXME: Replace with path to the jar containing Test.java | |
String url = "jar:file:///C:/Users/Johannes/loci/test/target/scijava-tests-2.43.1-SNAPSHOT.jar!/"; | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
URLClassLoader cl1 = new URLClassLoader(new URL[] { new URL(url) }); | |
Method m1 = cl1.loadClass("org.scijava.Test").getDeclaredMethod("printMe", null); | |
// NB: with this commented, loaded jar can not be deleted. | |
// cl1.close(); | |
while (!br.readLine().equals("end")) { | |
URLClassLoader cl = new URLClassLoader(new URL[] { new URL(url) }); | |
Class<?> cls = cl.loadClass("org.scijava.Test"); | |
Method m = cls.getDeclaredMethod("printMe", null); | |
System.out.print("M1 class: " + m1.getDeclaringClass() + " "); | |
m1.invoke(null, null); | |
System.out.print("M2 class: " + m.getDeclaringClass() + " "); | |
m.invoke(null, null); | |
// NB: with this commented, loaded jar can not be deleted. | |
// cl.close(); | |
} | |
} | |
} |
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 org.scijava; | |
public class Test { | |
public static void printMe() { | |
System.out.println("This is message 1"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment