Adding a JAR to a plugin's classpath dynamically [message #117429] |
Wed, 27 August 2003 12:32  |
Eclipse User |
|
|
|
Originally posted by: daniel_nospam.enigmatec.net
I've seen various references to this but I can't get it to work.
I create my own URLClassLoader using an array of URLs pointing to the JARs
I want to add and the existing classpath, then I set the classloader using
Thread.currentThread().setContextClassLoader(newClassLoader)
When I try to load a class from the JAR, even immediately after setting the
classloader above, I get a ClassNotFoundException.
I wonder if (a) my URLs are correctly formed (although I've tried a number
of ways) and (b) I'm setting the classloader correctly?
Any guidance with this would be most helpful... code follows:
private void refreshClasspath(Collection jars) {
IPluginDescriptor descriptor = RDKPlugin.getDefault().getDescriptor
();
URLClassLoader classLoader = ((PluginClassLoader)
(descriptor.getPluginClassLoader()));
List urls = new ArrayList();
try {
for(Iterator i = jars.iterator(); i.hasNext(); ) {
IFile file = ResourcesPlugin.getWorkspace().getRoot
().getFileForLocation(new Path((String) i.next()));
String path = file.getLocation().toString();
urls.add(new URL("file://" + path));
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
classLoader = URLClassLoader.newInstance((URL[]) urls.toArray(new URL
[urls.size()]), classLoader);
Thread.currentThread().setContextClassLoader(classLoader);
try {
Class c = Class.forName("foo.bar.HelloWorld");
System.err.println(c.toString());
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
}
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.06027 seconds