0

Currently, I am doing image loading from:

fi.xman3.assets. ( src/fi/xman3/assets/ )

With this code:

public static BufferedImage get(String path) {
    BufferedImage img = null;

    try {
        img = ImageIO.read(new File(path));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return img;
}

When I try to run the JAR file from cmd ( java -jar example.jar ), it just starts giving me IOException errors for "File doesn't exist".

Running from Eclipse works without any errors.

Mickael
  • 3,506
  • 1
  • 21
  • 33
  • 2
    If it is packed into a jar it is no longer considered a `File` but a `resource`. You need to go about doing something like this.. `URL imageURL = this.getClass().getResource("/fi/xman3/assets/image.png");` then do.. `ImageIcon t = new ImageIcon(imageURL);` – 3kings Sep 03 '16 at 20:21
  • 1
    @3kings you should turn that into an answer so it can be accepted and upvoted. :) – J Earls Sep 03 '16 at 20:33
  • 3kings, that doesn't seem to be working. Now I get an error saying "Cannot use this in a static context", when I highlight "this". –  Sep 03 '16 at 20:43
  • Alright, I did some fiddling and "ImageLoader.class.getResource(path);" seems to work. Thanks! –  Sep 03 '16 at 20:52

0 Answers0