Question: I have a problem with Java application, particular in loading a image from a location in my computer.

Following [this post](http://www.dreamincode.net/forums/topic/162999-java-image-manipulation-part-3-math-and-swingworker/) I used a `BufferedImage` and a `InputFileStream` to load an image on my computer. First, I put the image (`pic2.jpg`) into the source code and that is working. However, if I put the image to another place (let's say `C:\\ImageTest\pic2.jpg`), Java IDE show me an `IllegalArgumentException` 

```
return ImageIO.read(in);

```

here is the code:

```
public class MiddlePanel extends JPanel {
    private BufferedImage img;

    public MiddlePanel(int width) {    
        //img = getImage("pic2.jpg");       
        img = getImage("C:\\ImageTest\\pic2.jpg");

        this.setPreferredSize(new Dimension(800,460));

    }

    public void paintComponent(Graphics g) {
        // ...
    }

    private BufferedImage getImage(String filename) {
        // This time, you can use an InputStream to load
        try {
            // Grab the InputStream for the image.                    
            InputStream in = getClass().getResourceAsStream(filename);

            // Then read it.
            return ImageIO.read(in);
        } catch (IOException e) {
            System.out.println("The image was not loaded.");
            //System.exit(1);
        }

        return null;
    }
}

```

Answer: 
`getResource` & `getResourceAsStream` do not work with file paths, but paths relative the code base. If the code base is `C:` then a relative path that would locate the resource is `/ImageTest/pic2.jpg`.

> 
> ..difference between load file by `FileInputStream` and `getResourceAsStream`?
> 
> 
> 

One major difference is that the `getResource..` will work with a resource inside a Jar, which is no longer a `File`. Therefore `FileInputStream` *cannot* be used to access such a resource.
You cannot use `Class#getResource(String)` or `Class#getResourceAsStream(String)` in this case. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to `ClassLoader.getSystemResourceAsStream(java.lang.String)`.

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a `/` (`\u002f`), then the absolute name of the resource is the portion of the name following the `/`.
Otherwise, the absolute name is of the following form:
modified\_package\_name/name

Where the modified\_package\_name is the package name of this object with `/` substituted for `.` (`\u002e`).

Generally, it is not a good thing to hard code the system location of your resources in your code. The neat and clean way is to put your resources in your classpath and access them. Hope this clarifies why it's not working