KaiHH, I searched extensively before posting here, and have read the exact thread you linked to in its entirety several times.
Your next question might by why that thread wasn’t sufficient, so I’ll go into a little more detail here.
The answers in that thread fall into a few basic categories:
-
Use getProtectionDomain(). My concerns about this are that there are some caveats mentioned in the thread, and that it can throw a SecurityException. That may or may not be a problem in practice (I’m not sure), but one of my goals is to minimize the number of possible failure points, and in that sense getResource() seems a little more promising.
-
Use getResource(). There were some caveats mentioned with this approach as well, although I’m pretty confident they don’t apply in my particular case. The issue remains of decoding the URL and converting it to the needed form. Some of the answers skipped this part; one highly-rated answer seemed to address it, but I wasn’t able to get that method to work (which may have been user error, or I may have simply misunderstood what was being suggested).
-
Use ‘user.dir’. This of course will not give the desired results in all cases.
I’ll also mention that some of the answers seemed to have errors or be problematic in various ways. Some of the answers came with the caveat of only having been tested on certain platforms, and some seem to make more assumptions regarding the format of the returned URL than others. In short, it’s not just a matter of popping into that thread and finding the answer. There are probably 10 to 20 different specific solutions offered there, and much discussion about potential problems and pitfalls. Extracting a robust, reliable solution from that thread might seem trivial to you, but for me it’s taken some work (and I’m still somewhat uncertain about it).
The code I posted here is the solution I came up with after reading that thread and many others. I wanted to get some other eyes on it because I’m not confident I’ve covered every angle or necessarily understood everything I’ve read correctly. If it’s a trivial problem with a trivial solution, it should be easy to look over the three lines of code I posted and point out any problems with it.
Anyway, if anyone can pick the code apart, please do! I’d very much like to know if there’s anything wrong with it.