Java Class.getResource(String name)

Syntax

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

public URL getResource(String name)

Example

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


/*from   w  w w. j a  v a2  s .c  om*/
import java.net.URL;


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.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable