class loader:

Discussion in 'Java' started by Ulrich Scholz, Jan 30, 2006.

  1. I am still trying to load a class with a custom class loader. For more
    info on why I want to do that and how I got here, please refer to
    thread "class loader confusion".
    http://groups.google.com/group/comp...c55aa7/af208975da0f25f2?#doc_95f65ee8cd0dec18

    Now, I wrote the custom class loader MyDomainLoader as given below. (I
    tried to follow http://www.javalobby.org/java/forums/t18345.html)
    A call to loadClass(String) is redirected to findClass(String). This
    function (successfully) reads the class file into a byte array and
    calls defineClass in class ClassLoader. In turn, defineClass calles
    the native function defineClass1.

    My problem now is that defineClass1 does not simply returns the class
    represented by the byte array. Instead, a trace with eclipse tells me
    that it (recursively) calls loadClass(String,boolean).

    Does it or not? Why?

    I get more and more confused.

    Ulrich


    The problem

    final class MyDomainLoader extends ClassLoader
    {
    public MyDomainLoader()
    {
    super();
    }

    protected Class<?> loadClass(final String name, final boolean
    resolve)
    throws ClassNotFoundException
    {
    return findClass(name);
    }

    public Class<?> loadClass(final String name) throws
    ClassNotFoundException
    {
    return loadClass(name, false);
    }

    public Class<?> findClass(final String className){
    byte classByte[];

    String classString =
    className.replace('.',File.separatorChar) + ".class";
    String classPath = ((String)
    ClassLoader.getSystemResource(classString).getFile());

    try
    {
    classByte = loadClassData(classPath);
    }
    catch(IOException e)
    {
    throw(new PPException(IO_ERROR, "find class"));
    }

    return defineClass(className, classByte, 0,
    classByte.length, null);
    }

    private byte[] loadClassData(final String className) throws
    IOException{

    final File f = new File(className);
    final int size = (int)f.length();
    byte buff[] = new byte[size];
    FileInputStream fis = new FileInputStream(f);
    DataInputStream dis = new DataInputStream(fis);
    dis.readFully(buff);
    dis.close();
    return buff;
    }
    }
     
    Ulrich Scholz, Jan 30, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    553
    Andrea Desole
    Dec 14, 2005
  2. =?iso-8859-1?q?Andr=E9_Wagner?=

    Loading a class using the applet class loader

    =?iso-8859-1?q?Andr=E9_Wagner?=, May 30, 2007, in forum: Java
    Replies:
    7
    Views:
    442
    Mark Space
    May 31, 2007
  3. Karsten Wutzke
    Replies:
    0
    Views:
    389
    Karsten Wutzke
    Jun 29, 2007
  4. gorudonu
    Replies:
    0
    Views:
    1,007
    gorudonu
    Aug 5, 2010
  5. Replies:
    0
    Views:
    170
Loading...

Share This Page