Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading resources using getClass().getResource()

Tags:

java

swing

I am trying to load an image to use as an icon in my application. The appropriate method according to this tutorial is:

protected ImageIcon createImageIcon(String path, String description)  {     java.net.URL imgURL = getClass().getResource(path);     if (imgURL != null) {         return new ImageIcon(imgURL, description);     } else {         System.err.println("Couldn't find file: " + path);         return null;     } } 

So, I placed the location of the file, and passed it as a parameter to this function. This didn't work, i.e. imgURL was null. When I tried creating the ImageIcon by passing in the path explicitly:

ImageIcon icon  = new ImageIcon(path,"My Icon Image"); 

It worked great! So the application can pick up the image from an explicitly defined path, but didn't pick up the image using getResources(). In both cases, the value of the path variable is the same. Why wouldn't it work? How are resources found by the class loader?

Thanks.

like image 281
Luhar Avatar asked Feb 26 '10 16:02

Luhar


People also ask

How does getResource work in Java?

The getResourceAsStream method returns an InputStream for the specified resource or null if it does not find the resource. The getResource method finds a resource with the specified name. It returns a URL to the resource or null if it does not find the resource. Calling java.

How do you specify a resource path in Java?

The simplest approach uses an instance of the java. io. File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file.


2 Answers

getClass().getResource(path) loads resources from the classpath, not from a filesystem path.

like image 140
matt b Avatar answered Sep 27 '22 21:09

matt b


You can request a path in this format:

/package/path/to/the/resource.ext 

Even the bytes for creating the classes in memory are found this way:

my.Class -> /my/Class.class 

and getResource will give you a URL which can be used to retrieve an InputStream.

But... I'd recommend using directly getClass().getResourceAsStream(...) with the same argument, because it returns directly the InputStream and don't have to worry about creating a (probably complex) URL object that has to know how to create the InputStream.

In short: try using getResourceAsStream and some constructor of ImageIcon that uses an InputStream as an argument.

Classloaders

Be careful if your app has many classloaders. If you have a simple standalone application (no servers or complex things) you shouldn't worry. I don't think it's the case provided ImageIcon was capable of finding it.

Edit: classpath

getResource is—as mattb says—for loading resources from the classpath (from your .jar or classpath directory). If you are bundling an app it's nice to have altogether, so you could include the icon file inside the jar of your app and obtain it this way.

like image 24
helios Avatar answered Sep 27 '22 21:09

helios