URLClassLoader URLClassLoader(String, URL[], ClassLoader) in Java

URLClassLoader(String, URL[], ClassLoader): This method is available in the java.net.URLClassLoader class of Java.

Syntax:

java.net.URLClassLoader.URLClassLoader(String name, URL[] urls, ClassLoader parent)

This method takes three arguments. 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.

Any URL that ends with a '/' is assumed to refer to a directory.

Otherwise, the URL is assumed to refer to a JAR file which will be downloaded and opened as needed.

Parameters: Three parameters are required for this method.

name: class loader name; or null if not named.

urls: the URLs from which to load classes and resources.

parent: the parent class loader for delegation.

Throws:

1. IllegalArgumentException - if the given name is empty.

2. NullPointerException - if urls or any of its elements is null.

3. SecurityException - if a security manager exists and its SecurityManager

Approach 1: When no exception

Java

package com.URLClassLoader;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class URLClassLoader3 {
    public static void main(String[] args) throws MalformedURLException {

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

        System.out.println(urlClassLoader);
    }
}

Output:

java.net.URLClassLoader@763d9750


Approach 2: IllegalArgumentException 

Java

package com.URLClassLoader;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class URLClassLoader3 {
    public static void main(String[] args) throws MalformedURLException {

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

        System.out.println(urlClassLoader);
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: name must be non-empty or null at java.base/java.lang.ClassLoader.checkCreateClassLoader(ClassLoader.java:366) at java.base/java.lang.ClassLoader.<init>(ClassLoader.java:431) at java.base/java.security.SecureClassLoader.<init>(SecureClassLoader.java:114) at java.base/java.net.URLClassLoader.<init>(URLClassLoader.java:214) at com.URLClassLoader.URLClassLoader3.main(URLClassLoader3.java:15)


Approach 3: NullPointerException 

Java

package com.URLClassLoader;

import java.net.URL;
import java.net.URLClassLoader;

public class URLClassLoader3 {
    public static void main(String[] args) {

        URL urls[] = null;
        ClassLoader parent = ClassLoader.getPlatformClassLoader();
        String name = "hello";
        URLClassLoader urlClassLoader = new URLClassLoader(name, urls, parent);

        System.out.println(urlClassLoader);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "urls" is null at java.base/jdk.internal.loader.URLClassPath.<init>(URLClassPath.java:153) at java.base/jdk.internal.loader.URLClassPath.<init>(URLClassPath.java:174) at java.base/java.net.URLClassLoader.<init>(URLClassLoader.java:216) at com.URLClassLoader.URLClassLoader3.main(URLClassLoader3.java:12)


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