Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Adding a JAR to a plugin's classpath dynamically
Adding a JAR to a plugin's classpath dynamically [message #117429] Wed, 27 August 2003 12:32 Go to next message
Eclipse UserFriend
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();
}
}
Re: Adding a JAR to a plugin's classpath dynamically [message #117535 is a reply to message #117429] Wed, 27 August 2003 15:08 Go to previous messageGo to next message
Eclipse UserFriend
What eclipse version?

Have you looked at DelegatingURLClassLoader (and its calls to mungeJarURLs?)
Have you tried printing out the URLs you are modifying and sending back to
the URLClassLoader?

Interestingly, I am just doing something very similar, but I get a
NoClassDefFoundError :-)

Chris

"Dan Gravell" <daniel_nospam@enigmatec.net> wrote in message
news:Xns93E4B295A5322danielnospamenigmate@204.138.98.10...
> 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();
> }
> }
Re: Adding a JAR to a plugin's classpath dynamically [message #117604 is a reply to message #117535] Wed, 27 August 2003 15:47 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gentijo.eyecatching.com

I have been doing some work with the Transview project on Source forge
and the Endorsing Classloader is a good working example of using the
URL classloader.
-JG


Chris Laffra wrote:

> What eclipse version?
>
> Have you looked at DelegatingURLClassLoader (and its calls to mungeJarURLs?)
> Have you tried printing out the URLs you are modifying and sending back to
> the URLClassLoader?
>
> Interestingly, I am just doing something very similar, but I get a
> NoClassDefFoundError :-)
>
> Chris
>
> "Dan Gravell" <daniel_nospam@enigmatec.net> wrote in message
> news:Xns93E4B295A5322danielnospamenigmate@204.138.98.10...
>
>>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();
>>}
>>}
>
>
>
Re: Adding a JAR to a plugin's classpath dynamically [message #117641 is a reply to message #117604] Wed, 27 August 2003 16:53 Go to previous messageGo to next message
Eclipse UserFriend
URL?

"John Gentilin" <gentijo@eyecatching.com> wrote in message
news:3F4D0AE6.6000102@eyecatching.com...
> I have been doing some work with the Transview project on Source forge
> and the Endorsing Classloader is a good working example of using the
> URL classloader.
> -JG
>
>
> Chris Laffra wrote:
>
> > What eclipse version?
> >
> > Have you looked at DelegatingURLClassLoader (and its calls to
mungeJarURLs?)
> > Have you tried printing out the URLs you are modifying and sending back
to
> > the URLClassLoader?
> >
> > Interestingly, I am just doing something very similar, but I get a
> > NoClassDefFoundError :-)
> >
> > Chris
> >
> > "Dan Gravell" <daniel_nospam@enigmatec.net> wrote in message
> > news:Xns93E4B295A5322danielnospamenigmate@204.138.98.10...
> >
> >>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();
> >>}
> >>}
> >
> >
> >
>
Re: Adding a JAR to a plugin's classpath dynamically [message #117651 is a reply to message #117641] Wed, 27 August 2003 17:41 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gentijo.eyecatching.com

www.sourceforge.com then search transclipse.
I think it actually moce to another subproject but
there is a link.

Here is the CVS link
:pserver:anonymous@cvs.sourceforge.net:/cvsroot/eclipseproject

Chris Laffra wrote:
> URL?
>
> "John Gentilin" <gentijo@eyecatching.com> wrote in message
> news:3F4D0AE6.6000102@eyecatching.com...
>
>>I have been doing some work with the Transview project on Source forge
>>and the Endorsing Classloader is a good working example of using the
>>URL classloader.
>>-JG
>>
>>
>>Chris Laffra wrote:
>>
>>
>>>What eclipse version?
>>>
>>>Have you looked at DelegatingURLClassLoader (and its calls to
>
> mungeJarURLs?)
>
>>>Have you tried printing out the URLs you are modifying and sending back
>
> to
>
>>>the URLClassLoader?
>>>
>>>Interestingly, I am just doing something very similar, but I get a
>>>NoClassDefFoundError :-)
>>>
>>>Chris
>>>
>>>"Dan Gravell" <daniel_nospam@enigmatec.net> wrote in message
>>>news:Xns93E4B295A5322danielnospamenigmate@204.138.98.10...
>>>
>>>
>>>>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();
>>>>}
>>>>}
>>>
>>>
>>>
>
>
Re: Adding a JAR to a plugin's classpath dynamically [message #117662 is a reply to message #117651] Wed, 27 August 2003 17:58 Go to previous messageGo to next message
Eclipse UserFriend
We're Sorry.
The SourceForge.net Website is currently down for maintenance.
We will be back shortly


:-)


"John Gentilin" <gentijo@eyecatching.com> wrote in message
news:3F4D259E.90002@eyecatching.com...
> www.sourceforge.com then search transclipse.
> I think it actually moce to another subproject but
> there is a link.
>
> Here is the CVS link
> :pserver:anonymous@cvs.sourceforge.net:/cvsroot/eclipseproject
>
> Chris Laffra wrote:
> > URL?
> >
> > "John Gentilin" <gentijo@eyecatching.com> wrote in message
> > news:3F4D0AE6.6000102@eyecatching.com...
> >
> >>I have been doing some work with the Transview project on Source forge
> >>and the Endorsing Classloader is a good working example of using the
> >>URL classloader.
> >>-JG
> >>
> >>
> >>Chris Laffra wrote:
> >>
> >>
> >>>What eclipse version?
> >>>
> >>>Have you looked at DelegatingURLClassLoader (and its calls to
> >
> > mungeJarURLs?)
> >
> >>>Have you tried printing out the URLs you are modifying and sending back
> >
> > to
> >
> >>>the URLClassLoader?
> >>>
> >>>Interestingly, I am just doing something very similar, but I get a
> >>>NoClassDefFoundError :-)
> >>>
> >>>Chris
> >>>
> >>>"Dan Gravell" <daniel_nospam@enigmatec.net> wrote in message
> >>>news:Xns93E4B295A5322danielnospamenigmate@204.138.98.10...
> >>>
> >>>
> >>>>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();
> >>>>}
> >>>>}
> >>>
> >>>
> >>>
> >
> >
>
Re: Adding a JAR to a plugin's classpath dynamically [message #117710 is a reply to message #117429] Wed, 27 August 2003 21:00 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: pascal_rapicault.yahoo.fr

The ant.core plugin is doing something similar: creates a classloader, and
link it back to the plugin classloaders.
I do not have the name of the class, but it can be find easily

PaScaL

"Dan Gravell" <daniel_nospam@enigmatec.net> a
Re: Adding a JAR to a plugin's classpath dynamically [message #117757 is a reply to message #117710] Thu, 28 August 2003 00:37 Go to previous message
Eclipse UserFriend
org.eclipse.ant.internal.core.AntClassLoader

Darins

"Pascal Rapicault" <pascal_rapicault@yahoo.fr> wrote in message
news:bijk6h$rl8$1@eclipse.org...
> The ant.core plugin is doing something similar: creates a classloader, and
> link it back to the plugin classloaders.
> I do not have the name of the class, but it can be find easily
>
> PaScaL
>
> "Dan Gravell" <daniel_nospam@enigmatec.net> a
Previous Topic:Deadlock or similar problem with IResource.createMarker(...)
Next Topic:MutliPageEditorPart
Goto Forum:
  


Current Time: Tue Jul 22 13:08:37 EDT 2025

Powered by FUDForum. Page generated in 0.06027 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top