DerekMallon
DerekMallon

Reputation: 25

Can't load image - image not found

I am trying to load an image as part of my platformer, but it cant seam to find the image, I put it into the default package, which is the same as the class

public BufferedImage loadImage() {
  try {
    BufferedImage img = ImageIO.read(new File("level.jpg"));
    System.out.println("Level Found!");
    return img;

  } catch (IOException e) {
    System.out.println("Level Missing!");
    return null;
  }
}

Upvotes: 1

Views: 3018

Answers (2)

user3041058
user3041058

Reputation: 1548

Use get resource method of the class loader

Upvotes: 0

Boris Brodski
Boris Brodski

Reputation: 8695

If you reference your files like this new File("level.jpg") you have to put those files into the current directory. In most cases it's not the default package directory.

For example using Eclipse your typical directory structure would be like this:

+ MyProject
  |
  + src
    |
    + org/mypackage1
    + org/mypackage2

In order to get new File("level.jpg") to work you have to put level.jpg into the project root directory, like this

+ MyProject
  |
  + src
  | |
  | + org/mypackage1
  | + org/mypackage2
  |
  + level.jpg 

Upvotes: 3

Related Questions