Skip to content

Commit

Permalink
Update getThisJarPath
Browse files Browse the repository at this point in the history
  • Loading branch information
gurpreet- committed Jan 28, 2021
1 parent 1187307 commit 089d7a9
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/main/java/co/libly/resourceloader/ResourceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -425,43 +425,46 @@ protected boolean isPosixCompliant() {
public static URL getThisJarPath(final Class<?> c) {
if (c == null) return null; // could not load the class

// try the easy way first
// Try the easy way first
try {
final URL codeSourceLocation =
c.getProtectionDomain().getCodeSource().getLocation();
if (codeSourceLocation != null) return codeSourceLocation;
}
catch (final SecurityException e) {
// NB: Cannot access protection domain.
}
catch (final NullPointerException e) {
// NB: Protection domain or code source is null.
if (codeSourceLocation != null) {
return codeSourceLocation;
}
} catch (final SecurityException e) {
// Cannot access protection domain.
} catch (final NullPointerException e) {
// Protection domain or code source is null.
}

// NB: The easy way failed, so we try the hard way. We ask for the class
// The easy way failed, so we try the hard way. We ask for the class
// itself as a resource, then strip the class's path from the URL string,
// leaving the base path.

// get the class's raw resource path
// Get the class' raw resource path
final URL classResource = c.getResource(c.getSimpleName() + ".class");
if (classResource == null) return null; // cannot find class resource
if (classResource == null) {
return null; // cannot find class resource
}

final String url = classResource.toString();
final String suffix = c.getCanonicalName().replace('.', '/') + ".class";
if (!url.endsWith(suffix)) return null; // weird URL

// strip the class's path from the URL string
final String base = url.substring(0, url.length() - suffix.length());
if (!url.endsWith(suffix)) {
return null; // weird URL
}

String path = base;
// Strip the class's path from the URL string
String path = url.substring(0, url.length() - suffix.length());

// remove the "jar:" prefix and "!/" suffix, if present
if (path.startsWith("jar:")) path = path.substring(4, path.length() - 2);
// Remove the "jar:" prefix and "!/" suffix, if present
if (path.startsWith("jar:")) {
path = path.substring(4, path.length() - 2);
}

try {
return new URL(path);
}
catch (final MalformedURLException e) {
} catch (final MalformedURLException e) {
e.printStackTrace();
return null;
}
Expand Down

0 comments on commit 089d7a9

Please sign in to comment.