Home » Java programming language

Java ClassLoader getSystemResources() method with example

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

ClassLoader Class getSystemResources() method

  • getSystemResources() method is available in java.lang package.
  • getSystemResources() method is used to find all the system resources of the given resource name from the searching location to load classes.
  • getSystemResources() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get any error.
  • getSystemResources() method may throw an exception at the time of returning URL.
    IOException: This exception may throw when I/O operation performs.

Syntax:

    public static Enumeration getSystemResources (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 the following values based on the given cases,

  • It returns the Enumeration of URL when system resources associated with the given name exists.
  • It returns null, when no system resource associated with the given name exists.

Example:

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

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

public class GetSystemResourcesOfClass {
    public static void main(String[] args) throws Exception {
        // Get Class by using forName() method
        Class cl = Class.forName("GetSystemResourcesOfClass");

        // Get ClassLoader by using ClassLoader
        ClassLoader loader = cl.getClassLoader();

        // It return Enumeration of URL objects with the given //resource name
        Enumeration en = loader.getSystemResources("getProperties().doc");

        // Display address of the resource
        System.out.print("Enum of System Resources : ");

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

Output

Enum of System Resources : file:/E:/Programs/getProperties().doc


Comments and Discussions!

Load comments ↻





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