1

So I'm writing my gui in Java and have managed to sort out the loading and resizing of various images that I have in my /src/resources folder.

However what I'd like to be able to do is use images that havent been included in the source code. I want my program to be able to grab an image from its current directory.

The image is used for a company logo, and I'd like other users to be able to change the logo without me having to recompile the whole thing.

I'm having a hard time finding anything on this topic, if anyone can help me out I'd be very grateful.

Cheers

  • 2
    The current directory, and the directory where the jar file is, are different directories. The current directory is the directory from which the java command is executed. What do you *actually* want? – JB Nizet Oct 29 '17 at 09:41
  • You could use a system property to point to a directory from which to load these images (and it could default to the current working directory or a `.myapp` directory in the users home directory or something like that). – Thilo Oct 29 '17 at 09:43
  • 1
    `ImageIO.read(new File("nameOfImage.png"))` will load the named image from the current "working" directory. The trick here is, the "working" directory may not be the same directory the "jar file" is located. In these situations, it's always better to store the images in a "well known" location, like the `user.dir`. Have a look at [Where should i place my files in order to be able to access them when i run the jar?](https://stackoverflow.com/questions/27974857/where-should-i-place-my-files-in-order-to-be-able-to-access-them-when-i-run-the/27974989#27974989) for more specific details – MadProgrammer Oct 29 '17 at 09:44
  • Ok so if my compiled .jar is in a folder called MyProgram. I'd like to be able to load images from that same directory. – Lawrence Holmes Oct 29 '17 at 09:45
  • 1
    Possible duplicate of [Get location of JAR file](https://stackoverflow.com/questions/15359702/get-location-of-jar-file) – BackSlash Oct 29 '17 at 09:46
  • Thanks MadProgrammer, I'll have a read. – Lawrence Holmes Oct 29 '17 at 09:47
  • Then the easiest and safest way is probably to provide a .sh and/or .bat file to start your program, which makes sure to make this directory the current directory (i.e. to `cd` into that directory) and/or to pass that directory as argument or system property to your program. Such a .sh/.bat file is able to know what its own location is. – JB Nizet Oct 29 '17 at 09:50
  • Thanks for the help guys. I think I'll be able to sort it out with this advice. – Lawrence Holmes Oct 29 '17 at 11:08

0 Answers0