Home » Java programming language

Java ClassLoader getSystemResource() method with example

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

ClassLoader Class getSystemResource() method

  • getSystemResource() method is available in java.lang package.
  • getSystemResource() method is used to find the system resource of the given resource name from the searching location to load classes.
  • getSystemResource() 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.
  • getSystemResource() method does not throw an exception at the time of returning URL.

Syntax:

    public URL getSystemResource (String resource_name);

Parameter(s):

  • String resource_name – represents the name of the resource.

Return value:

The return type of this method is URL, it returns the following values based on the given cases,

  • It returns the URL when any system resource 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 URL getSystemResource (String resource_name) method 
// of ClassLoader 

import java.net.*;

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

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

        // It return URL with the given resource name
        URL address = loader.getSystemResource("E://Programs//getProperties.doc");

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

Output

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


Comments and Discussions!

Load comments ↻





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