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"));
Asked
Active
Viewed 259 times
1

Justin Loos
- 11
- 1
-
1You 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 Answers
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
-
For in/jar do you literally say in/jar or like in/"jarname.extension" ? – Justin Loos Mar 05 '12 at 22:30
-
If the path within the jar was "/images/filename.jpg", then I would use that as the argument. – Michael Brewer-Davis Mar 05 '12 at 23:01
-
Ok thanks, this is my 2nd year of java and haven't gotten very far past my schools subset. – Justin Loos Mar 05 '12 at 23:46