Bug fixes and startup checks
[debian/openrocket] / src / net / sf / openrocket / util / JarUtil.java
1 package net.sf.openrocket.util;
2
3 import java.io.File;
4 import java.net.URI;
5 import java.net.URISyntaxException;
6 import java.net.URL;
7 import java.security.CodeSource;
8
9 import net.sf.openrocket.database.Database;
10
11 public class JarUtil {
12
13         /**
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.
16          * 
17          * @return      a File object of the current Java archive, or <code>null</code>
18          */
19         public static File getCurrentJarFile() {
20                 // Find the jar file this class is contained in
21                 URL jarUrl = null;
22                 CodeSource codeSource = Database.class.getProtectionDomain().getCodeSource();
23                 if (codeSource != null)
24                         jarUrl = codeSource.getLocation();
25                 
26                 if (jarUrl == null) {
27                         return null;
28                 }
29                 return urlToFile(jarUrl);
30         }
31         
32         
33
34         public static File urlToFile(URL url) {
35                 URI uri;
36                 try {
37                         uri = url.toURI();
38                 } catch (URISyntaxException e) {
39                         try {
40                                 uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), 
41                                                 url.getPort(), url.getPath(), url.getQuery(), url.getRef());
42                         } catch (URISyntaxException e1) {
43                                 throw new IllegalArgumentException("Broken URL: " + url);
44                         }
45                 }
46                 return new File(uri);
47         }
48         
49         
50 }