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.


    public URL getResource(String resource_name);


  • 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.


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


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 : ");


URL of Resource : null


Comments and Discussions



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

© some rights reserved.