1

I have managed to find some similar topics to this one, but nothing so far has matched this question exactly. I am making a JavaFX application where the user can add images to be displayed in the application. These images are stored as filepath strings in a text-file database. Since .jar files are read-only, I can't add the image directly into the project, so I have resorted to creating an assets folder that sits just outside the project that the images all sit in.

So the folder structure is:

-> Parent Folder that the .jar file sits in
    - Project Folder or .jar file 
        - src
            * classes.java
    - assets Folder
        * image.jpg
        * image2.png

Now, I know that Image image = new Image(getClass().getResourceAsStream(imagePath)); will work if my imagePath is inside the project folder. But I need the path relative (and outside) the folder.

My first (and, for now, most important) question is how do I state this relative path that breaks out of the folders of the application altogether?

Secondly, is this good practice? If one is making a JavaFX app and wishes to add new images to be stored with the app, is there a better way of doing it?

  • 1
    You have use a File reference with a relative path to you’re directory - the problem with this is, the working directory may change, will cause you no end of issues – MadProgrammer Dec 13 '17 at 23:27
  • Your _project directory_ is completely irrelevant — what you care about is the _current working directory_ of the program _at runtime_. When your program is running this will generally be `../` but, as MadProgrammer says, the current directory _can_ change during program execution. – Stephen P Dec 13 '17 at 23:32
  • 1
    An overall better choice is to place the files in a well known location (under the user's `home` directory), specifically will depend on the OS. A better solution for readonly assets is to store them within the Jar – MadProgrammer Dec 13 '17 at 23:55
  • Thanks, this makes much more sense. I'm now using the `home` directory. – markbackhouse Dec 13 '17 at 23:57

1 Answers1

2

So leveraging Getting the Current Working Directory in Java to break out of your jar you can use FileInputStreams to get your Images. For instance

String cwd = System.getProperty("user.dir");
File dirAboveCws = new File(cwd).getParentFile();
File[] imageFiles = dirAboveCws.listFiles(new FileFilter() {
    @Override
    public boolean accept(File pathname) {
        return !pathname.getName().contains("jpg");
    }
});

for (File imageFile : imageFiles) {
    FileInputStream fileInputStream = new FileInputStream(imageFile);
    Image image = ImageIO.read(fileInputStream);  // Here is your image
}

As far as whether or not it's a good idea it kinda sucks that your images storage is dependent on the current working directory. IMO you should use a dot-prefixed subfolder of the user's home directory (System.getProperty("user.home")) to store your application data

benashby
  • 468
  • 2
  • 11
  • Thank you! Both appear to work nicely, but you're right, it's much more sensible to start from the bottom of the file system rather than trying to reach upwards, not knowing whether anything lies there! So I've changed to reflect your final sentence. – markbackhouse Dec 13 '17 at 23:56