-1

I'm developing a Java SE 11 Maven project. In my project I have a folder called assets (it's a sibling of the src folder). This folder contains several folders and files. The whole folder has to be available at the application path - during development (in Netbeans) AND when it is deployed via an MSI package.

When running in the IDE, my assets live in <projectRoot>/assets, while my current working dir is <projectRoot>. And that's ok. I can access the assets folder via ./assets from my current working directory.

But when I deploy my application, the current working directory might be something else, but not the application folder. So accessing the assets folder via ./assets will fail.

What's the best approach in Java SE, to access a folder within the application's folder, independent of the current working directory?

Turning the assets into a resource folder is not an option, because the files must editable by the user. The assets folder has to reside directly in the file system.

René Pöpperl
  • 567
  • 5
  • 18
  • rename it to `resources` and it will end up in your jar. – tgdavies Aug 05 '22 at 12:31
  • Sorry, I forgot to mention, that this is not an option in this case. See my edit above. – René Pöpperl Aug 05 '22 at 12:40
  • When the application is deployed, is the `assets` folder created as a subfolder of the root folder of the deployment? For example, if I deploy your application (on Windows) to `C:\Users\Abra`, will there be a `C:\Users\Abra\assets` folder? – Abra Aug 05 '22 at 12:45
  • `System.getProperty("user.dir")` will return the working directory. – Abra Aug 05 '22 at 12:48
  • There will be a folder like C:\Program Files\App\assets. But when my current working directory is D:\somewhere I can't access my folder using `./assets` because that would give me `D:\somewhere\assets` ... – René Pöpperl Aug 05 '22 at 12:50
  • What about defining an environment variable that stores the root folder of the deployed application like `ANT_HOME` which is required by [Apache Ant](https://ant.apache.org/manual/install.html) ? – Abra Aug 05 '22 at 12:56
  • How are you creating your MSI? I think there environment variables/ System properties that will tell you what the executable is. – matt Aug 05 '22 at 12:58
  • there is also user.home https://stackoverflow.com/a/16239152/2067492 – matt Aug 05 '22 at 13:01
  • I'd store the assets as resources, but when the program starts, check for a `/Appassets` directory, and if it doesn't exist, (i.e. the first time we start) create it and copy the resources to that directory and afterwards use them. – tgdavies Aug 07 '22 at 11:46
  • 1
    This would be very easy with `jpackage` application, but not easy to answer unless you give more info on the packager and what settings your application defines. The app ought to copy the reference version of `assets` to a user storage if not exists there. However there are plenty of different env settings which might be relevant depending on your circumstances and platforms. eg copy to `Path.of(System.getenv(XYZ), "assets")`) where XYZ = "LOCALAPPDATA" | "APPDATA" | "user.home" being a just a few of the choices. – DuncG Aug 07 '22 at 13:10

2 Answers2

0

If you put your files/resources inside your source folder, you can access them through ClassName.getResource() and ClassName.getResourceAsStream() methods.

0

use a config file or maven profiles (local and production) in that config or profile define a folder where the files are

old anwser: use Resources, the files in the jar (i assume your application is in a jar) will be accessible using the

URL url = Main.class.getResource("filename")

or

Inputstream stream = Main.class.getResourceAsStream("filename")

replace 'Main' with your classname

https://docs.oracle.com/javase/8/docs/technotes/guides/lang/resources.html

https://www.tutorialspoint.com/java/lang/class_getresourceasstream.htm

Jvo
  • 46
  • 4