View Single Post
  #2 (permalink)  
Old 07-17-2007, 12:12 PM
JavaBean's Avatar
JavaBean JavaBean is offline
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
This is a compiler array. The compiler complains because it can not find the class you want to use but i understand that you want to load that class dynamically without knowing its implementation details!

As far as i see, there is only one solution:

1. You will create an interface and include that interface to this project. Lets call it TestInterface.

2. The interface will have all the methods you want to call from your TestClass.

3. Your TestClass will implement this interface. (e.g. TestClass implements TestInterface)

4. Modify your code and cast your dynamically created class into TestInterface object (not TestClass object). Like this:

Code:
SimpleClassLoader simp = new SimpleClassLoader(); try { Class c = simp.loadClass("TestClass"); TestInterface t = null; try { t = (TestInterface) c.newInstance(); } catch (IllegalAccessException ex) { } catch (InstantiationException ex) { } System.out.println(t.toString()); System.out.println(c.getName()); } catch (ClassNotFoundException e) { e.printStackTrace(); }
5. You will successfully compile your code in this case and use the methods you want to call from your project without knowing implementation details of TestClass.
Reply With Quote