Applets
Locating Resource
Creating a URL from a Class Reference
When a class is loaded, the JVM creates a meta-object for the class, which can be obtained using
java.lang.Class metaObject = this.getClass();
The Class class provides access to useful information about the class, such as the data fields, constructors, and methods. It also contains the getResource(filename) method, which can be used to obtain the URL of a given file name in the class directory.
To obtain the URL of a file in the class directory, use
URL url = metaObject.getResource(filename);
For example, suppose the class directory is c:\book, the following statements create a URL for c:\book\image\us.gif.
Class metaObject = this.getClass(); URL url = metaObject.getResource("image/us.gif");
You can now create an ImageIcon using
ImageIcon imageIcon = new ImageIcon(url);