Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Custom ClassLoader = frustration & pain.

807580Jun 30 2010 — edited Jul 5 2010
Short version : I need to override the findLibrary() method of ClassLoader so I can load .dlls from a .jar. I can't simply loadLibrary() or load() them because when one particular .dll is loaded (j3dcore-ogl-cg.dll) it insists on attempting to load its dependents. If I have control of findLibrary(), then I can load them from wherever I want.

The problem is, I can't see my ClassLoader in use. Here's the skeleton of what I'm using as the ClassLoader :
public class Test extends ClassLoader
{
	public static void main(String[] args)
	{
		System.out.println("Test parent class loader = "+Test.class.getClassLoader());

		Test l = new Test(
			Test.class.getClassLoader()
		);


		System.out.println("Custom class loader = "+l);

		try
		{
			Class<?> test2 = l.loadClass("Test2", true);
			Method main = test2.getMethod("main", Array.newInstance(String.class, 0).getClass());
			main.invoke(null, new Object[] {args});
		}

		catch (SecurityException e)		{	e.printStackTrace();	}
		catch (NegativeArraySizeException e)	{	e.printStackTrace();	}
		catch (IllegalArgumentException e)	{	e.printStackTrace();	}
		catch (ClassNotFoundException e)		{	e.printStackTrace();	}
		catch (NoSuchMethodException e)		{	e.printStackTrace();	}
		catch (IllegalAccessException e)		{	e.printStackTrace();	}
		catch (InvocationTargetException e)	{	e.printStackTrace();	}
	}


	Test(ClassLoader parent)
	{
		super(parent);
	}
}
and here's the class I'm loading :
public class Test2
{
	public static void main(String[] args)
	{
		System.out.println("Daughter class loader = "+Test2.class.getClassLoader());
	}
}
The parent of my ClassLoader is
sun.misc.Launcher$AppClassLoader@11b86e7
which I guess is the system class loader.


My custom loader identifies as
Test@3e25a5
However, the daughter class that's loaded identifies its class loader as
sun.misc.Launcher$AppClassLoader@11b86e7
Why is this? Why isn't my class loader being used to load the class? I'm developing in Eclipse - would that make a difference?


Thanks for any pointers, and apologies for any stupid mistakes.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 2 2010
Added on Jun 30 2010
9 comments
227 views