Results 1 to 2 of 2
  1. #1
    snooze-g is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Default 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.



    Java 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");
    
    [COLOR="Red"]            TestClass t = null;[/COLOR]
    
                try {
                    [COLOR="Red"]t = (TestClass) c.newInstance();[/COLOR]           
     } 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.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    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:

    Java 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.

Similar Threads

  1. Will this applet ever work?
    By willemjav in forum Java Applets
    Replies: 4
    Last Post: 04-20-2008, 06:40 PM
  2. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 03:58 AM
  3. what do I have to install to work with JSP
    By boy22 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-05-2007, 05:08 AM
  4. Program don't work
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-04-2007, 10:51 PM
  5. eclipse classloader problem
    By sandor in forum Eclipse
    Replies: 2
    Last Post: 05-10-2007, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •