

Note that both getResource("") and getResource(".") failed in my tests, when the class resided within a JAR file both invocations returned null. So I recommend the #2 invocation shown above instead, as it seems safer. Step 2: URL to FileĮither way, once you have a URL, the next step is convert to a File. This is its own challenge see Kohsuke Kawaguchi's blog post about it for full details, but in short, you can use new File(url.toURI()) as long as the URL is completely well-formed. Lastly, I would highly discourage using URLDecoder. Some characters of the URL, : and / in particular, are not valid URL-encoded characters. It is assumed that all characters in the encoded string are one of the following: "a" through "z", "A" through "Z", "0" through "9", and "-", "_", ".", and "*". The character "%" is allowed but is interpreted as the start of a special escaped sequence. There are two possible ways in which this decoder could deal with illegal strings.

It could either leave illegal characters alone or it could throw an IllegalArgumentException. Which approach the decoder takes is left to the implementation.

In practice, URLDecoder generally does not throw IllegalArgumentException as threatened above. And if your file path has spaces encoded as %20, this approach may appear to work. To achieve these steps, you might have methods like the following: /** However, if your file path has other non-alphameric characters such as + you will have problems with URLDecoder mangling your file path. * "/path/to/my-jar.jar!/my/package/M圜lass.class") then it will return the * If the class is within a JAR file (e.g., * "/path/to/my/package/M圜lass.class") then it will return the base directory * If the class is directly on the file system (e.g., * Gets the base location of the given class. * path to the JAR (e.g., "file:/path/to/my-jar.jar"). Public static URL getLocation(final Class c) * FileUtils#urlToFile(URL) to convert the result to a File}. * IllegalArgumentException if the URL does not correspond to a file.Windows sucks! We all know that. I am using the Maven appassembler plugin to create a deployable distribution for my Rmi Server. It was complaining that the path was too long! bat file that is generated by the appassembler, suddenly wont launch.

Well, I visited the plugin’s home page and after struggling for 3 hrs, I was none the wiser. Then, I chanced upon an article and with a little bit of tweaking, I was able to get it working.
