Unable to access resources outside the package in Java

Viewed 170

I am currently developing a game, and have encountered a very burdensome problem.

I want to draw a picture on the screen but every time I try to read the resource / picture I get NULL. I tried 2 kinds of methods to read the picture but still couldn't.

But when I moved the image into the package that contains the class from which I was trying to read the image the image appeared.

So the problem is that i just can't access resources outside of the current package. And i need to know how can i do that, how can i access this resource. It has to be a resource that I can use even after exporting the game to a JAR file.

My Project Structure

Code I tried (The first one is buffered image and the second is just image type:

    try {
        image = ImageIO.read(getClass().getResource("blocks.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    --------------------------------------------------------------

    image = new ImageIcon(getClass().getResource("blocks.png))).getImage();

I hope the question is clear enough.

1 Answers

Try creating a package called "resources", instead of a folder, and then access your image as "resources/blocks.png".

Related