I know there are quite a few answers for accessing the resources directory in Java JARs, but my problem is slightly different because I am trying to get the absolute URL of a parent directory in the resources folder instead of reading a file.
Here is the project structure:
project
|_ src
|_ main
|_ java
|_ SomeCode.java
|_ resources
|_ en-us.lproj
|_ Localizable.strings
Essentially, there are some localisation files located in the resources folder. Then, somewhere in the source code, I access these files by doing:
URL url = getClass().getClassLoader().getResource("en-us.lproj/Localizable.strings");
URL baseDirectory = url.toURI().resolve("..").toURL();
LocalizationResourceBundles fileLoader = new LocalizationResourceBundles(baseDirectory.getPath());
Note that LocalizationResourceBundles is an external API that I do not have the right to modify. This particular method takes in a path to the parent directory holding the localisation folders + files (i.e. /resources in this case) and loads the localisation files accordingly.
So if I run this locally (e.g. in the IDE), everything works well. But when I package the application into a Java JAR and then run the JAR, the calls fail with the exception:
java.lang.IllegalArgumentException: URI is not absolute
at java.base/java.net.URL.fromURI(URL.java:692)
at java.base/java.net.URI.toURL(URI.java:1116)
...
which makes sense since resources may be packaged differently when they are JAR-ed. But now I am at a loss - how can I get this working both locally and in a JAR without modifying LocalizationResourceBundles? Is this even achievable with how Java JARs are organised?