1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package javax.jdo;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.lang.reflect.Method;
23 import java.net.URL;
24 import java.net.URLClassLoader;
25
26 public class ClasspathHelper {
27
28 private static URLClassLoader SYSTEM_CLASSLOADER = (URLClassLoader) ClassLoader.getSystemClassLoader();
29 private static Method METHOD;
30 static {
31 try {
32 METHOD = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
33 METHOD.setAccessible(true);
34 }
35 catch (Throwable t) {
36 throw new RuntimeException(t);
37 }
38 }
39
40 public static void addFile(String s) throws IOException {
41 addFile(s, null);
42 }
43
44 public static void addFile(File f) throws IOException {
45 addFile(f, null);
46 }
47
48 public static void addURL(URL u) throws IOException {
49 addURL(u, null);
50 }
51
52 public static void addFile(String s, URLClassLoader loader) throws IOException {
53 addFile(new File(s), loader);
54 }
55
56 public static void addFile(File f, URLClassLoader loader) throws IOException {
57 addURL(f.toURL(), loader);
58 }
59
60 public static void addURL(URL u, URLClassLoader loader) throws IOException {
61 if (loader == null) {
62 loader = SYSTEM_CLASSLOADER;
63 }
64 try {
65 METHOD.invoke(loader, new Object[] { u });
66 }
67 catch (Throwable t) {
68 throw new IOException("Could not add URL to system classloader: " + t.getMessage());
69 }
70 }
71 }
72
73