ClassLoader.getSystemResource() Method



Description

The java.lang.ClassLoader.getSystemResource() method find a resource of the specified name from the search path used to load classes.

Declaration

Following is the declaration for java.lang.ClassLoader.getSystemResource() method

public static URL getSystemResource(String name)

Parameters

name − This is the resource name.

Return Value

This method returns a URL object for reading the resource, or null if the resource could not be found.

Exception

NA

Example

The following example shows the usage of java.lang.ClassLoader.getSystemResource() method.

package com.tutorialspoint;

import java.lang.*;
import java.net.URL;

public class ClassLoaderDemo {

   public static void main(String[] args) throws Exception {
     
      Class cls = Class.forName("ClassLoaderDemo");

      // returns the ClassLoader object associated with this Class
      ClassLoader cLoader = cls.getClassLoader();
    
      System.out.println(cLoader.getClass());
    
      // finds resource
      URL url = cLoader.getSystemResource("file.txt");
      System.out.println("Value = " + url);

      // finds resource
      url = cLoader.getSystemResource("newfolder/a.txt");
      System.out.println("Value = " + url);  
   }
} 

Let us compile and run the above program, this will produce the following result −

class sun.misc.Launcher$AppClassLoader
Value = file:/C:/Program%20Files/Java/jdk1.6.0_06/bin/file.txt
Value = null
java_lang_classloader.htm
Advertisements