File result = null;
if (resourceURL != null) {
URI uri = resourceURL.toURI();
String scheme = uri.getScheme();
String ssp = uri.getSchemeSpecificPart();
if (scheme.equals("jar")) {
/*
*The scheme-specific part will look like "file:<file-spec>!/<path-to-class>.class"
*so we need to isolate the scheme and the <file-spec> part.
*The subscheme (the scheme within the jar) precedes the colon
*and the file spec appears after it and before the exclamation point.
*/
int colon = ssp.indexOf(':");
String subscheme = ssp.substring(0, colon);
int excl = ssp.indexOf('!");
String containingJarPath = ssp.substring(colon + 1, excl);
result = new File(containingJarPath);
} else {
throw new IllegalArgumentException(resourceURL.toExternalForm());
}
}
return result;