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
icon5.gif  Dynamic loading of jar files using custom classLoader [message #514844] Tue, 16 February 2010 20:26 Go to next message
Sönke Holsten is currently offline Sönke HolstenFriend
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 (available here). 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.doSomething();	        
	        }
		} 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!

[Updated on: Wed, 17 February 2010 11:07]

Report message to a moderator

Re: Dynamic loading of jar files using custom classLoader [message #515132 is a reply to message #514844] Wed, 17 February 2010 20:33 Go to previous messageGo to next message
Bruce Kelly is currently offline Bruce KellyFriend
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!
Re: Dynamic loading of jar files using custom classLoader [message #516619 is a reply to message #515132] Wed, 24 February 2010 15:37 Go to previous messageGo to next message
Sönke Holsten is currently offline Sönke HolstenFriend
Messages: 6
Registered: February 2010
Junior Member
Thank you for pointing me in the right direction. Using custom class loaders in an OSGi framework the same way as one would usually do in a java project is indeed simply not possible. There was a bug report about that, which discusses possible workarounds, have a look here.
Re: Dynamic loading of jar files using custom classLoader [message #604789 is a reply to message #515132] Wed, 24 February 2010 15:37 Go to previous message
Sönke Holsten is currently offline Sönke HolstenFriend
Messages: 6
Registered: February 2010
Junior Member
Thank you for pointing me in the right direction. Using custom class loaders in an OSGi framework the same way as one would usually do in a java project is indeed simply not possible. There was a bug report about that, which discusses possible workarounds, have a look https://bugs.eclipse.org/bugs/show_bug.cgi?id=3074
Previous Topic:Feature-based self-hosting
Next Topic:How to provide parameters to the OSGI resolver during headless build
Goto Forum:
  


Current Time: Sat Dec 20 01:34:57 GMT 2014

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

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