This is strange...

> Well, now the open connections should be closed when the classloaders
> are garbage collected. I m writing a test case now to get that theroy
> commited.

I wrote a little testclass and it seems that _any_ resource once touched
by an URLConnection cannot be removed on Windows anymore?!

\Daniel

See yourself:

import java.net.*;
import java.io.*;



public class Test
{
    public static void main (String[] _args) throws Exception
    {
        if (_args.length != 2)
        {
            System.out.println ("usage: Test file_url_to_jar_package
file_path_in_the_package");
            System.out.println ("Attention: the file_url_to_jar_package
gets deleted (eventually ;-))");
            System.exit(0);
        }


        URL[] urls = new URL[] {new URL (_args[0])};
        String res = _args[1];

/*
///////////////////////////////////////////////////////////////////////////////////////
        System.out.println ("now trying to delete file: "+ new File
(urls[0].getFile()).delete());
///////////////////////////////////////////////////////////////////////////////////////
*/

/*
///////////////////////////////////////////////////////////////////////////////////////
        ClassLoader cl = new URLClassLoader (urls,
                                            
Thread.currentThread().getContextClassLoader());
        System.out.println ("created classloader.");

        InputStream in = cl.getResourceAsStream (res);
///////////////////////////////////////////////////////////////////////////////////////
*/

///////////////////////////////////////////////////////////////////////////////////////
        InputStream in = new URL ("jar:"+urls[0]+"!/"+res).openStream
();
///////////////////////////////////////////////////////////////////////////////////////

        System.out.println ("created resource input stream.");

        byte[] read = new byte[512];
        int length = in.read (read);
        in.close ();

        if (length < 512)
            System.out.println ("read whole resource into mem.");
        else
            System.out.println ("read first 512 byte of the resource
into mem.");

        System.out.println ("now trying to delete file: "+ new File (new
URL (_args[0]).getFile()).delete());

/*
///////////////////////////////////////////////////////////////////////////////////////
        System.out.println ("setting classloader to null.");
        cl = null;
        System.out.println ("now trying to delete file: "+ new File
(urls[0].getFile()).delete());
///////////////////////////////////////////////////////////////////////////////////////
*/

///////////////////////////////////////////////////////////////////////////////////////
        System.out.println ("setting url to null.");
        urls = null;
        System.out.println ("now trying to delete file: "+ new File (new
URL (_args[0]).getFile()).delete());
///////////////////////////////////////////////////////////////////////////////////////


        System.out.println ("do garbage collection...");
        System.gc ();
        System.out.println ("now trying to delete file: "+ new File (new
URL (_args[0]).getFile()).delete());
    }
}


Reply via email to