/** Closes the loader and jar file */ public final synchronized void close() { if (System.getProperty("java.version") .startsWith("1.6")) { // Java 6 doesn't have the URLClassLoader.close() method try { // Insert Reflection Magic Class<?> clazz = URLClassLoader.class; Field ucpField = clazz.getDeclaredField("ucp"); // get field for sun.misc.URLClassPath ucpField.setAccessible(true); // Allow access Object ucp = ucpField.get(this); // get URLClassPath instance Field loadersField = ucp.getClass().getDeclaredField("loaders"); // get the loaders collection loadersField.setAccessible(true); // Allow access Object loaders = loadersField.get(ucp); // get loaders for (Object jarLoader : ((Collection<?>) loaders)) { // iterate the loaders try { Field jarField = jarLoader.getClass().getDeclaredField("jar"); // Get the jarField jarField.setAccessible(true); // Allow access Object jarFile = jarField.get(jarLoader); // get the JarFile ((JarFile) jarFile).close(); // Close jar } catch (Throwable t) { // Not a loader, ignored... } } } catch (Throwable t) { // probably not a SUN/Oracle VM } } else { // If running on Java 7 or above, call URLClassLoader.close(). Since we should be // compiling with Java 6, lower versions shouldn't be a worry try { // We have to invoke the method since we compile with Java 6 (or should be) URLClassLoader.class.getDeclaredMethod("close").invoke(this); } catch (Exception ex) { // Probably IOException, ignore it. } } ccw.removeLoader(this); // And finally remove url and classes from the jar }