tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Google Guava > IO > Classpath Resource

Classpath Resource 

Google Guava is a java library with lot of utilities and reusable components. This requires the library guava-10.0.jar to be in classpath. The following example shows using Resources.getResource() API. It returns the URL of given classpath resource if found otherwise throws IllegalArgumentException.

File Name  :  
com/bethecoder/tutorials/guava/io_tests/ClassPathResourceTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.guava.io_tests;

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

import com.google.common.io.Resources;

public class ClassPathResourceTest {

  /**
   @param args
   @throws IOException 
   */
  public static void main(String[] argsthrows IOException {
    
    URL fileURL = Resources.getResource("abc.txt");
    System.out.println(fileURL);

    //Instead of using
    //ClassPathResourceTest.class.getClassLoader().getResource()
    URL clsURL = Resources.getResource(
        "com/bethecoder/tutorials/guava/io_tests/ClassPathResourceTest.class");
    System.out.println(clsURL);
  }

}
   

It gives the following output,
file:/C:/BTC/GUAVA_LIB001/bin/abc.txt
file:/C:/BTC/GUAVA_LIB001/bin/
	com/bethecoder/tutorials/guava/io_tests/ClassPathResourceTest.class



 
  


  
bl  br