JDK-4823678 : Reading a jar URL prevents jarfile deletion on windows
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.4.1,5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2003-02-25
  • Updated: 2019-09-17
  • Resolved: 2017-08-16
Related Reports
Relates :  
Description
Name: nt126004			Date: 02/25/2003


FULL PRODUCT VERSION :
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)


FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
If you read data from a jarfile using a jar url, you can no
longer delete the jarfile. Somewhere internally a stream to
the jarfile must be being kept open, preventing the deletion.

It seems this is only an issue on windows machines as I have
been unable to reproduce the problem on Solaris 8 or Redhat
7.2.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run the attached program on Windows.

EXPECTED VERSUS ACTUAL BEHAVIOR :
After closing the input stream from the url, it should be
possible to delete the jarfile.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class JarURLTest {

  public static void main(String []args) throws Exception {
    // Create a wee zipfile
    File mTmp = File.createTempFile("JarFileTest",".zip");
    ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(mTmp));
    zout.putNextEntry(new ZipEntry("blah.txt"));
    zout.write("hello".getBytes());
    zout.close();

    // Read from the entry using a jar url
    // Commenting out the following four lines allows the file to be deleted
    URL u = new URL("jar:" + mTmp.toURL() + "!/blah.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(u.openStream()));
    System.err.println("Read \"" + br.readLine() + "\" from " + u.toString());
    br.close();

    // Now attempt to delete the file.
    if (!mTmp.delete()) {
      System.err.println("Failed to delete file!!");
    }
    
  }
}

---------- END SOURCE ----------
(Review ID: 181418) 
======================================================================

Comments
As of Java 9, the URLConnection API provides a method of configuring caching per-protocol scheme. The default is still to cache, but cache control is now at a finer level of granularity.
16-08-2017

EVALUATION Will address for a later release. ###@###.### 2003-03-07
07-03-2003