From d00e70c0a9fcb0c764c9cb5211afb7320211585d Mon Sep 17 00:00:00 2001 From: kruland2607 Date: Thu, 26 Sep 2013 11:08:13 -0500 Subject: [PATCH] Added some logging and an additional mechanism to try to find the jar file URL. --- core/src/net/sf/openrocket/util/JarUtil.java | 39 +++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/core/src/net/sf/openrocket/util/JarUtil.java b/core/src/net/sf/openrocket/util/JarUtil.java index 545cfa581..57a21e85d 100644 --- a/core/src/net/sf/openrocket/util/JarUtil.java +++ b/core/src/net/sf/openrocket/util/JarUtil.java @@ -1,15 +1,22 @@ package net.sf.openrocket.util; import java.io.File; +import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.security.CodeSource; +import java.util.Enumeration; import net.sf.openrocket.database.Database; -public class JarUtil { +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +public class JarUtil { + + private final static Logger logger = LoggerFactory.getLogger(JarUtil.class); + /** * Return the a File object pointing to the JAR file that this class belongs to, * or null if it cannot be found. @@ -20,16 +27,40 @@ public class JarUtil { // Find the jar file this class is contained in URL jarUrl = null; + CodeSource codeSource; try { codeSource = new URL("rsrc:.").openConnection().getClass().getProtectionDomain().getCodeSource(); + logger.debug("Found jar file using rsrc URL"); } catch (Throwable e) { codeSource = Database.class.getProtectionDomain().getCodeSource(); } - if (codeSource != null) + if (codeSource != null) { + logger.debug("Found jar file using codeSource"); jarUrl = codeSource.getLocation(); + } + if (jarUrl == null) { + try { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + Enumeration urls = cl.getResources("README.TXT"); + if (!urls.hasMoreElements()) { + return null; + } + URL readmeURL = urls.nextElement(); + jarUrl = readmeURL; + String urlString = readmeURL.toString(); + // cut off the trailing path component: + urlString = urlString.substring(0, urlString.length() - "!/README.TXT".length()); + // cut off the prefix jar: + urlString = urlString.substring("jar:".length()); + logger.debug("jar file location using README.TXT is {}", urlString); + jarUrl = new URL(urlString); + logger.debug("Found jar file using README.TXT location"); + } catch (IOException e1) { + } + } if (jarUrl == null) { return null; } @@ -40,15 +71,13 @@ public class JarUtil { return null; } - - public static File urlToFile(URL url) { URI uri; try { uri = url.toURI(); } catch (URISyntaxException e) { try { - uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), + uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); } catch (URISyntaxException e1) { throw new IllegalArgumentException("Broken URL: " + url);