Home » Java programming language

Java ClassLoader findResources() method with example

ClassLoader Class findResources() method: Here, we are going to learn about the findResources() method of ClassLoader Class with its syntax and example.
Submitted by Preeti Jain, on November 27, 2019

ClassLoader Class findResources() method

  • findResources() method is available in java.lang package.
  • findResources() method is used to find all the resources with the given resource name in the enumeration of URL objects.
  • findResources() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • findResources() method may throw an exception at the time of finding the resources.
    IOException: This exception may throw during I/O error.

Syntax:

    protected Enumeration findResources(String resource_name);

Parameter(s):

  • String resource_name – represents the name of the resource.

Return value:

The return type of this method is Enumeration, it returns an enumeration of URL object for the given resources.

Example:

// Java program to demonstrate the example 
// of Enumeration findResources(String resource_name)
// method of ClassLoader 

import java.util.*;
import java.io.*;

class FindResources extends ClassLoader {
    void findResources() {
        try {
            // It checks whether the given resources is found
            // or not by using the findResources()
            Enumeration en = super.findResources("getProperties().doc");

            // If en not null that means en found
            // then don't need to load again
            if (en != null)
                System.out.println("Resources Found: " + en.toString());
            else
                System.out.println("Resources Not Found!!!");
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        // Creating an instance of FindResources
        FindResources fr = new FindResources();
        fr.findResources();
    }
}

Output

Resources Found: java.util.Collections$EmptyEnumeration@512ddf17


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.