import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

public class JI9036344 {

	public static void main(String[] args) throws IOException {
		// Generate a jar file with a file inside 
		File jarFile = File.createTempFile("test", ".jar"); 
		try { 
			try (JarOutputStream jarOutput = new JarOutputStream(new FileOutputStream(jarFile))) { 
				jarOutput.putNextEntry(new JarEntry("file.txt")); 
				jarOutput.write("Hello World".getBytes("UTF-8")); 
				jarOutput.closeEntry(); 
			} 

			// Define two different instances of URLClassLoader from the same set of urls. 
			URL[] urls = new URL[]{ jarFile.toURI().toURL() }; 
			URLClassLoader cl1 = new URLClassLoader(urls, null); 
			URLClassLoader cl2 = new URLClassLoader(urls, null); 

			// Open a resource stream from the first CL 
			try (InputStream is = cl1.getResourceAsStream("file.txt")) { 
				System.out.println("First stream: " + is); 
			} 

			// Open another resource stream from the second CL, with the same resource name 
			try (InputStream is = cl2.getResourceAsStream("file.txt")) { 
				System.out.println("Second stream: " + is); 
				while (is.read() >= 0) { 
					// While reading, close the first CL, the next is.read() will throw a "java.io.IOException: Stream closed" 
					cl1.close(); 
				} 
			} 

			cl2.close(); 
		} finally { 
			jarFile.delete(); 
		} 

	}

}
