I try to add an image to JPanel. It works if I specify the full name of the image (including name of all folders). But I want my software to be machine independent (another machine does not have the same folder structure as my machine has). So, I put my image to the same place where the source is and in my code I use just name of the image (no folders' names). It does not work. I put my image into the folder where my executables are. It does not work again. So, why it does not work and what can I do with that?
+3
A:
Relative file names are resolved relative to the current directory of the application (as defined by the System property user.dir
), not necessarily its source/jar location.
If you want to load images (or other resources) that you deliver together with your application, then it's best to put them into the .jar file of your application and load them using ClassLoader
.getResource()
and/or getResourceAsStream()
.
Joachim Sauer
2010-04-26 08:49:07