Home » Java programming language

Java Class class getResource() method with example

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

Class class getResource() method

  • getResource() method is available in java.lang package.
  • getResource() method is used to return the URL (Uniform Resource Locator) with the given resource name.
  • getResource() 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.
  • getResource() method does not throw an exception at the time of returning URL.

Syntax:

    public URL getResource(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 resource associated with the given name exists.
  • It returns null, when no resource associated with the given name exists.

Example:

// Java program to demonstrate the example 
// of URL getResource(String resource_name) method of Class 

import java.net.*;

public class GetResourceOfClass {
 public static void main(String[] args) {
  GetResourceOfClass resource = new GetResourceOfClass();
  Class cl = resource.getClass();

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

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

Output

URL of Resource : null



Comments and Discussions!

Load comments ↻






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