I know this is a commonly asked question, but after doing some research on it I still have some doubts.
Say I have a directory structure like this:
parent_folder
--MyApp.jar
In order to make some documents distributed with the game more easily accessible to the user, I’d like to get the absolute path of parent_folder
. It seems like the most reliable method for this is to use getResource(), as in the following code (error checking omitted for brevity):
String path = Main.class.getResource("Main.class").getPath();
path = path.substring(0, path.indexOf("/MyApp.jar!"));
path = new URL(path).toURI().getPath();
Am I on the right track here? Can anyone spot any problems with this approach, or any cases where it might fail?