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 p开发者_开发问答lace 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?
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()
.
精彩评论