Java Reflection - Java Class.getResource(String name)


Class.getResource(String name) has the following syntax.

public URL getResource(String name)


In the following code shows how to use Class.getResource(String name) method.

//  w w w.  j  a va 2s.  c  o  m

public class Main {

  public static void main(String[] args) throws Exception {
    Main c = new Main();
    Class cls = c.getClass();

    URL url = cls.getResource("file.txt");
    System.out.println("Value = " + url);


The code above generates the following result.