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: [email protected]






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.