[1162] in java-interest

home help back first fref pref prev next nref lref last post

help with class loader

daemon@ATHENA.MIT.EDU (Michael Mills 21728)
Fri Aug 25 06:33:27 1995

From: ekim@nyquist.bellcore.com (Michael Mills 21728)
To: java-interest@java.Eng.Sun.COM
Date: Thu, 24 Aug 1995 12:35:30 -0400 (EDT)

Hi,


I am trying to learn how to download code using the class loader.
I cannot seem to figure out how to get things to work.
I have 2 problems.

1) How do you compile a java program when a referenced class is not local


2) How does the class loader work.


Here is the program I am working with. The hello class was created from a file
called Hello.java and hello.class was renamed to hello. This was to "simulate"
getting a class over a network. 

This line compiles but causes a crash:
                Object h = hello.newInstance();

And this one won't compile unless the hello class is available.
                hello hi = new hello();

q.java:68: Class hello not found in type declaration.
                hello hi = new hello();
                ^
q.java:68: Class hello not found in new.
                hello hi = new hello();



Any help is appreciated.


Mike


import java.lang.*;
import java.io.FileInputStream;
import java.util.Hashtable;



class tstLoader extends ClassLoader {
        int maxBuff = 32768;
        byte[] buff = new byte[maxBuff];
        static Hashtable cache = new Hashtable();

        public synchronized Class loadClass( String name)
        {
                Class c = (Class) cache.get(name);
                if( c == null )
                {
                        System.out.println("READ BYTES" );
                        int index=0;
                        FileInputStream fin = new FileInputStream( name );
                        int ret = 0;
                        while (ret >= 0)
                        {
                                ret = fin.read(buff,index,maxBuff-index);
                                if( ret > 0) index += ret;

                        }
                        fin.close();
                        System.out.println("DEFINE CLASS" );
                        c = defineClass(buff,0,index);
                        cache.put( new String(name),c);
                }
                return( c );
        }

        public synchronized Class loadClass(String name, boolean resolve)
        {

                System.out.println("LOAD CLASS name,resolve" );
                Class c = (Class) cache.get(name);
                if(c == null)
                {
                        System.out.println("TRY Class.forName" );
                        c = Class.forName(name);
                        if( c == null)
                        {
                                System.out.println("CLASS NOT FOUND" );
                        }
                } else {
                        System.out.println("HAD  Class cached" );
                }
                if( (c != null) && resolve )
                {
                        System.out.println("RESOLVE");
                        resolveClass(c);
                }
                return c;
        }

}
class tst
{
        public static void main (String argv [])
        {
                tstLoader loader = new tstLoader();
                // hello hi = new hello();
                Class  hello = loader.loadClass("hello");
                Object h = hello.newInstance();
                // hello hi = new hello();
        }


}
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post