URLClassLoader close() in Java

close(): This method is available in the java.net.URLClassLoader class of Java.

Syntax:

void java.net.URLClassLoader.close() throws IOException

This method closes this URLClassLoader so that it can no longer be used to load new classes or resources that are defined by this loader. Classes and resources defined by any of this loader's parents in the delegation hierarchy are still accessible. Also, any classes or resources that are already loaded, are still accessible.

In the case of jar: and file: URLs, it also closes any files that were opened by it. If another thread is loading a class when the close method is invoked, then the result of that load is undefined.

The method makes a best-effort attempt to close all opened files, by catching IOExceptions internally. Unchecked exceptions and errors are not caught.

Calling close on an already closed loader has no effect.

Parameters: NA

Returns: NA

Throws:

1. IOException - if closing any file opened by this class loader resulted in an IOException. Any such exceptions are caught internally. If only one is caught, then it is re-thrown. If more than one exception is caught, then the second and following exceptions are added as suppressed exceptions of the first one caught, which is then re-thrown.

2. SecurityException - if a security manager is set, and it denies RuntimePermission("closeClassLoader").

Approach

Java

package com.URLClassLoader;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;

public class URLClassLoaderclose {
    public static void main(String[] args) throws IOException {

        String protocol = "http", host = "localhost", file = "/hello";
        URL url = new URL(protocol, host, file);
        URL urls[] = { url };
        ClassLoader parent = ClassLoader.getPlatformClassLoader();

        URLStreamHandlerFactory factory = new URLStreamHandlerFactory() {

            @Override
            public URLStreamHandler createURLStreamHandler(String protocol) {
                return null;
            }
        };
        String name = "/hello";
        URLClassLoader urlClassLoader = new URLClassLoader(name, urls, parent, factory);

        urlClassLoader.close();
        System.out.println("Successfully closed");

    }
}

Output:

Successfully closed


Some other methods of URLClassLoader class

URLClassLoader(URL[])This method constructs a new URLClassLoader for the specified URLs using the default delegation parent ClassLoader.

URLClassLoader(URL[], ClassLoader)This method constructs a new URLClassLoader for the given URLs.

URLClassLoader(String, URL[], ClassLoader)This method constructs a new named URLClassLoader for the specified URLs.The URLs will be searched in the order specified for classes and resources after first searching in the specified parent class loader.

URLClassLoader(URL[], ClassLoader, URLStreamHandlerFactory)This method constructs a new URLClassLoader for the specified URLs, parent class loader, and URLStreamHandlerFactory. The parent argument will be used as the parent class loader for delegation. The factory argument will be used as the stream handler factory to obtain protocol handlers when creating new jar URLs.

URLClassLoader(String, URL[], ClassLoader, URLStreamHandlerFactory)This method constructs a new named URLClassLoader for the specified URLs, parent class loader, and URLStreamHandlerFactory.

close()This method closes this URLClassLoader so that it can no longer be used to load new classes or resources that are defined by this loader. 

findResource(String)This method finds the resource with the specified name on the URL search path.

findResources(String) This method returns an Enumeration of URLs representing all of the resources on the URL search path having the specified name.

getResourceAsStream(String)This method returns an input stream for reading the specified resource. If this loader is closed, any resources this method opens will be closed.

getURLs()This method returns the search path of URLs for loading classes and resources.

No comments:

Post a Comment