View Single Post
  #1 (permalink)  
Old 07-17-2007, 09:38 AM
snooze-g snooze-g is offline
Member
 
Join Date: Jul 2007
Posts: 8
snooze-g is on a distinguished road
My own ClassLoader didn't work.
Hi all.
I have a problem when I trying to get a instance of my TestClass object.
System get me a "cannot find symbol...".
I think that i have a problem with resolveClass method, as i know that method going to set Link to my class.
Please help.

Thanks Snooze-g.



Code:
import java.io.FileInputStream; import java.util.Hashtable; public class SimpleClassLoader extends ClassLoader { private Hashtable classes = new Hashtable(); public SimpleClassLoader() { } private byte getClassImplFromDataBase(String className)[] { System.out.println(" >>>>>> Fetching the implementation of " + className); byte result[]; try { FileInputStream fi = new FileInputStream("c:\\bindata\\" + className + ".class"); result = new byte[fi.available()]; fi.read(result); return result; } catch (Exception e) { return null; } } public Class loadClass(String className) throws ClassNotFoundException { return (loadClass(className, true)); } public synchronized Class loadClass(String className, boolean resolveIt) throws ClassNotFoundException { Class result; byte classData[]; System.out.println(" >>>>>> Load class : " + className); result = (Class) classes.get(className); if (result != null) { System.out.println(" >>>>>> returning cached result."); return result; } try { result = super.findSystemClass(className); System.out.println( " >>>>>> returning system class (in CLASSPATH)."); return result; } catch (ClassNotFoundException e) { System.out.println(" >>>>>> Not a system class."); } classData = getClassImplFromDataBase(className); if (classData == null) { throw new ClassNotFoundException(); } result = defineClass(className, classData, 0, classData.length); if (result == null) { throw new ClassFormatError(); } if (resolveIt) { resolveClass(result); } classes.put(className, result); System.out.println(" >>>>>> Returning newly loaded class."); return result; } public static void main(String[] args) { SimpleClassLoader simp = new SimpleClassLoader(); try { Class c = simp.loadClass("TestClass"); TestClass t = null; try { t = (TestClass) c.newInstance(); } catch (IllegalAccessException ex) { } catch (InstantiationException ex) { } System.out.println(t.toString()); System.out.println(c.getName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }

Last edited by snooze-g : 07-17-2007 at 11:45 AM.
Reply With Quote
Sponsored Links