Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Dynamic loading of jar files using custom classLoader
Dynamic loading of jar files using custom classLoader [message #604706] Tue, 16 February 2010 20:26 Go to next message
Sönke Holsten is currently offline Sönke Holsten
Messages: 6
Registered: February 2010
Junior Member
I've been searching for possibilities to dynamically load jar files for quite a while and came across a custom classLoader I like ( http://www.javaworld.com/javaworld/javatips/jw-javatip70.htm l). I downloaded the resources from that site (JarClassLoader.java, JarResources.java and MultiClassLoader.java) and put them to use in a small test project which worked out fine. However I want to use the JarClassLoader inside an Eclipse plug-in and for some reason I haven't been able to simply reuse the code from the test project for that purpose. The code I'm using in both the java test project and in the plug-in project is the following:

public static void run() {
JarClassLoader jarLoader = new JarClassLoader ("C:\\somepath\\testjar.jar");
try {
Class c = jarLoader.loadClass ("TestClass", true);
Object o = c.newInstance();
if (o instanceof TestClass) {
System.out.println("works...");
TestClass tc = (TestClass) o;
String test = tc.run();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

In the normal java project I have a main method which just calls the method run() and does nothing else. In the Eclipse plug-in project I have an action that when executed also just calls the method run() and nothing else. The problem is that in the Eclipse plug-in the check whether the created object is an instance of the class I desire fails, that is "works..." is never even printed. Although in debug mode it says that the object o is actually an instance of the class TestClass.

Can anyone point me to what I might be doing wrong?

Thank you for your help!
Re: Dynamic loading of jar files using custom classLoader [message #604721 is a reply to message #604706] Wed, 17 February 2010 20:33 Go to previous message
Bruce Kelly is currently offline Bruce Kelly
Messages: 63
Registered: July 2009
Member
I think the problem is you are comparing class objects created by two
different class loaders, and by definition they are not equal.

The use of TestClass by the class that is using the JarClassLoader means
that the class object for TestClass used in the instanceof expression is
loaded by the class loader for the class using the JarClassLoader.

When you use the JarClassLoader, it creates a different class object for the
same class.

I would be very wary of using my own class loader in an Eclipse plug-in due
to the fact that the plug-in is running in an OSGi framework.

Namaste, Bruce

<dsahalo@gmx.net> wrote in message news:hlev2f$54u$1@build.eclipse.org...
> I've been searching for possibilities to dynamically load jar files for
> quite a while and came across a custom classLoader I like
> ( http://www.javaworld.com/javaworld/javatips/jw-javatip70.htm l). I
> downloaded the resources from that site (JarClassLoader.java,
> JarResources.java and MultiClassLoader.java) and put them to use in a
> small test project which worked out fine. However I want to use the
> JarClassLoader inside an Eclipse plug-in and for some reason I haven't
> been able to simply reuse the code from the test project for that purpose.
> The code I'm using in both the java test project and in the plug-in
> project is the following:
>
> public static void run() {
> JarClassLoader jarLoader = new JarClassLoader
> ("C:\\somepath\\testjar.jar");
> try {
> Class c = jarLoader.loadClass ("TestClass", true);
> Object o = c.newInstance(); if (o instanceof TestClass) {
> System.out.println("works...");
> TestClass tc = (TestClass) o; String test =
> ; }
> } catch (ClassNotFoundException e) {
> e.printStackTrace();
> } catch (InstantiationException e) {
> e.printStackTrace();
> } catch (IllegalAccessException e) {
> e.printStackTrace();
> } }
> In the normal java project I have a main method which just calls the
> method run() and does nothing else. In the Eclipse plug-in project I have
> an action that when executed also just calls the method run() and nothing
> else. The problem is that in the Eclipse plug-in the check whether the
> created object is an instance of the class I desire fails, that is
> "works..." is never even printed. Although in debug mode it says that the
> object o is actually an instance of the class TestClass.
> Can anyone point me to what I might be doing wrong?
>
> Thank you for your help!
Previous Topic:Adding an item to the Source/generateGroup????
Next Topic:headless build issues/bugs
Goto Forum:
  


Current Time: Thu Oct 23 02:36:46 GMT 2014

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

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