View Javadoc

1   package org.sf.jlaunchpad.util;
2   
3   // ClassPathHacker.java
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          /* File.toURL() was deprecated, so use File.toURI().toURL() */
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                          /* Class was uncheched, so used URLClassLoader.class instead */
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                  // Retrieve the current classpath
44                  ClassLoader currentClassLoader = ClassPathHacker.class.getClassLoader();
45  
46                  // Convert myClassPath to an URL
47                  URL classpathURL = null;
48                  try {
49                    File fileClasspath = new File(path);
50                  classpathURL = fileClasspath .toURL();
51                  }
52                  catch (MalformedURLException ex)
53                  {
54                  // handle the exception
55                  }
56  
57                  // Construct the new URL
58                  URL [] urlClasspathArray = new URL [1];
59                  urlClasspathArray[0] = classpathURL;
60  
61                  // Create the new classloader
62                  URLClassLoader newClassLoader = new URLClassLoader(urlClasspathArray,currentClassLoader);
63  
64                  // Load the class
65                  Class theClass = null;
66                  try {
67                  theClass = newClassLoader.loadClass(myClass);
68                  }
69                  catch (ClassNotFoundException ex)
70                  {
71                  // handle the exception
72                  }
73  
74                  // At this step,
75                  // Either an exception has been thrown (MalformedURLException or
76                  // ClassNotFoundException)
77                  // or the class "myClass" is loaded.
78          }
79  
80  
81  }