Home » Java programming language

Java ClassLoader getResources() method with example

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

ClassLoader Class getResources() method

  • getResources() method is available in java.lang package.
  • getResources() method is used to identify all the resources with the given resource name.
  • getResources() 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.
  • getResources() method may throw an exception at the time of returning resource.
    IOException: This exception may throw during I/O operations.

Syntax:

    Enumeration getResources(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 Enumeration of URL object for scanning the resource otherwise it returns null when the given resource does not exist.

Example:

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

import java.net.*;
import java.util.*;

public class GetResourcesOfClassLoader {
 public static void main(String args[]) throws Exception {

  // It loads the class 
  Class cl = Class.forName("GetResourcesOfClassLoader");

  // It returns the class loader associated with 
  // the given class
  ClassLoader loader = cl.getClassLoader();

  // Display Loader Class
  System.out.println("Loader Class : ");
  System.out.println(loader.getClass());

  System.out.println();

  // It returns the resources associated with this Class
  // GetResourcesOfClassLoader
  Enumeration en = loader.getResources("getProperties().doc");

  // Display Resources
  System.out.println("Class Resources : ");

  while (en.hasMoreElements())
   System.out.println(en.nextElement());
 }
}

Output

Loader Class : 
class jdk.internal.loader.ClassLoaders$AppClassLoader

Class Resources : 


Comments and Discussions!

Load comments ↻





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