tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Articles > Java > Basics > How to load resources from Java Classpath

How to load resources from Java Classpath 

The following example shows how to load resources from classpath. Java provides the following APIs to get the class path resource as URL or Stream.

  • java.lang.Class
    public URL getResource(String paramString)
    public InputStream getResourceAsStream(String paramString)
     
  • java.lang.ClassLoader
    public URL getResource(String paramString)
    public InputStream getResourceAsStream(String paramString)
    public Enumeration<URL> getResources(String paramString)
Assume the following directory hierarchy.
C:\LoadResourceTest\top_level_prop.txt
C:\LoadResourceTest\com\bethecoder\tutorials\core\gen\local_prop.txt
C:\LoadResourceTest\com\bethecoder\tutorials\core\gen\LoadClassPathResource.java
C:\LoadResourceTest\com\bethecoder\tutorials\core\gen\LoadClassPathResource.class

C:\LoadResourceTest2\top_level_prop.txt
C:\LoadResourceTest3\top_level_prop.txt

File Name  :  
com/bethecoder/tutorials/core/gen/LoadClassPathResource.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.core.gen;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

public class LoadClassPathResource {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    /**
     * Access the file in the same directory as class
     * Ex: <PWD>/com/bethecoder/tutorials/core/gen/local_prop.txt
     */
    String fileToSearch = "local_prop.txt";
    URL fileURL = LoadClassPathResource.class.getResource(fileToSearch);
    
    System.out.println("");
    System.out.println("Resource '" + fileToSearch + "' is found at : " + fileURL);
    System.out.println("");
    
    /**
     * Access the file in the root directory of class
     * Ex: <PWD>/top_level_prop.txt
     */
    fileToSearch = "top_level_prop.txt";
    fileURL = LoadClassPathResource.class.getClassLoader().getResource(fileToSearch);
    System.out.println("Resource '" + fileToSearch + "' is found at : " + fileURL);
    System.out.println("");
    
    /**
     * Load resource from application class path
     */
    try {
      Enumeration<URL> urls = ClassLoader.getSystemResources(fileToSearch);
      if (urls.hasMoreElements()) {
        System.out.println("Resource '" + fileToSearch + "' is found in system resources");
      else {
        System.out.println("Resource '" + fileToSearch + "' is not found in system resources");
      }
      
      System.out.println();
      
      while (urls.hasMoreElements()) {
        System.out.println("Resource '" + fileToSearch + "' is found in system resources at : " + urls.nextElement());
      }
      
    catch (IOException e) {
      e.printStackTrace();
    }
    
  }

}
   

Execute the commands shown below and observe the output,

java com.bethecoder.tutorials.core.gen.LoadClassPathResource
java -cp C:\LoadResourceTest3;C:\LoadResourceTest2;. 
		com.bethecoder.tutorials.core.gen.LoadClassPathResource
From the output we can observe,
  • LoadClassPathResource.class.getResource() can access resources relative to the directory of current class.
  • LoadClassPathResource.class.getClassLoader().getResource() can access resources relative to the root directory of class.
  • ClassLoader.getSystemResources() can access the resources from system class path. The order of resources returned by this API depends on the order of classpath entries.

access_as_cls

Make a JAR out of LoadClassPathResource.class and property files. Execute the commands shown below and observe the output,

java -cp test.jar com.bethecoder.tutorials.core.gen.LoadClassPathResource
java -cp C:\LoadResourceTest3;C:\LoadResourceTest2;test.jar 
		com.bethecoder.tutorials.core.gen.LoadClassPathResource

access_as_jar


 
  


  
bl  br