1

I am creating a game as a project and I have gone through the code and debugged it. The problem I'm having is making a jar run with the code. I know that the error is the pathway to the images are wrong when I export it as a runnable jar file (exported using eclipse) but I don't know what the pathway would be to put in my ImageIO.read(new File("pathway/filename.jpg"));

  • 1
    You should be loading the images as resources, not as files. – Dave Newton Mar 05 '12 at 22:15
  • I'm not sure i understand the question, but i think you're looking for ClassLoader.getResourceAsStream() - http://stackoverflow.com/questions/793213/getting-the-inputstream-from-a-classpath-resource-xml-file – theglauber Mar 05 '12 at 22:18

1 Answers1

2

See "Accessing Resources" from the documentation.

You want some variant of ClassLoader.getResource(), though whether you access via the classloader or the class depends on how you want to package things. Here's what I usually do:

InputStream resourceStream 
                  = MyClass.class.getResourceAsStream("/pathway/in/jar/filename.jpg");
BufferedImage image = ImageIO.read(resourceStream);
Michael Brewer-Davis
  • 14,018
  • 5
  • 37
  • 49