1 package net.sf.openrocket.util;
5 import java.net.URISyntaxException;
7 import java.security.CodeSource;
9 import net.sf.openrocket.database.Database;
11 public class JarUtil {
14 * Return the a File object pointing to the JAR file that this class belongs to,
15 * or <code>null</code> if it cannot be found.
17 * @return a File object of the current Java archive, or <code>null</code>
19 public static File getCurrentJarFile() {
20 // Find the jar file this class is contained in
23 CodeSource codeSource;
25 codeSource = new URL("rsrc:.").openConnection().getClass().getProtectionDomain().getCodeSource();
26 } catch (Throwable e) {
27 codeSource = Database.class.getProtectionDomain().getCodeSource();
30 if (codeSource != null)
31 jarUrl = codeSource.getLocation();
37 File file = urlToFile(jarUrl);
45 public static File urlToFile(URL url) {
49 } catch (URISyntaxException e) {
51 uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(),
52 url.getPort(), url.getPath(), url.getQuery(), url.getRef());
53 } catch (URISyntaxException e1) {
54 throw new IllegalArgumentException("Broken URL: " + url);