1 package org.sf.jlaunchpad.util;
2
3
4
5 import java.lang.reflect.*;
6 import java.io.*;
7 import java.net.*;
8
9 public class ClassPathHacker {
10 private static final Class[] parameters = new Class[]{URL.class};
11
12 public static void addFile(String s) {
13 File f = new File(s);
14 addFile(f);
15 }
16
17
18 public static void addFile(File f) {
19 try {
20 addURL(f.toURI().toURL());
21 }
22 catch (Exception e) {
23 e.printStackTrace();
24 }
25 }
26
27 public static void addURL(URL u) {
28 URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
29 try {
30
31 Method method = URLClassLoader.class.getDeclaredMethod("addURL",parameters);
32 method.setAccessible(true);
33 method.invoke(sysloader,new Object[]{u});
34 System.out.println("Dynamically added " + u.toString() + " to classLoader");
35 }
36 catch (Exception e) {
37 e.printStackTrace();
38 }
39 }
40
41 public static void addResource(String myClass, String path) {
42
43
44 ClassLoader currentClassLoader = ClassPathHacker.class.getClassLoader();
45
46
47 URL classpathURL = null;
48 try {
49 File fileClasspath = new File(path);
50 classpathURL = fileClasspath .toURL();
51 }
52 catch (MalformedURLException ex)
53 {
54
55 }
56
57
58 URL [] urlClasspathArray = new URL [1];
59 urlClasspathArray[0] = classpathURL;
60
61
62 URLClassLoader newClassLoader = new URLClassLoader(urlClasspathArray,currentClassLoader);
63
64
65 Class theClass = null;
66 try {
67 theClass = newClassLoader.loadClass(myClass);
68 }
69 catch (ClassNotFoundException ex)
70 {
71
72 }
73
74
75
76
77
78 }
79
80
81 }