Creating custom ClassLoader.

Discussion in 'Java' started by Frank Rouse, Jun 18, 2004.

  1. Frank Rouse

    Frank Rouse Guest

    Hello All,

    I am trying to create my own classloader that will load a different
    version of a class from a jar file. I create a new thread and subclass
    URLClassloader and override the loadClass method such that it will
    search in the given jar first for the class and return that.

    Tracing through the execution all seems to go well. The loadClass
    method returns what seems to be a valid class but when I attempt to use
    the class I receive an InstantiationExeception. I have confirmed that
    the URL is valid so I know that it is not an issue. I also created a
    wide open java.policy file just in case it was something security related.

    The relevent code is below. I'd appreciate any pointers that anyone
    can give.

    Cheers


    ****************************************
    Relevent portion of thread code
    ****************************************

    public final void run() {
    URL[] codeLLocation = new URL[1];
    codeLLocation[0] = codeLocation;
    NewURLClassLoader loader = new NewURLClassLoader(codeLLocation);
    Thread.currentThread().setContextClassLoader(loader);
    Object o = null;
    while (keepRunning) {
    if (o == null) {
    try {
    System.out.println("The parent classloader = " + loader.getParent());
    Class c = loader.loadClass("<full_package_name_of_object");
    System.out.println("Classloader = " + c.getClassLoader());
    Object o = c.newInstance(); <===== Exception here.


    ****************************************
    Extended URLClassLoader
    ****************************************

    package org.fhtc.jde.threadtest;
    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.net.URL;
    import java.net.URLClassLoader;
    import java.util.jar.JarEntry;
    import java.util.jar.JarFile;
    public class NewURLClassLoader extends URLClassLoader {
    URL[] urls;
    NewURLClassLoader(URL[] inputURLS) {
    super(inputURLS);
    urls = inputURLS;
    }
    public Class findClass(String name) throws ClassNotFoundException {
    byte[] b = loadClassData(name);
    if (b == null) {
    throw (new ClassNotFoundException());
    }
    Class c = super.defineClass(name, b, 0, b.length);
    return c;
    }
    public synchronized Class loadClass(String name)
    throws ClassNotFoundException {
    // First, check if the class has already been loaded
    Class c = findLoadedClass(name);
    if (c == null) {
    try {
    c = this.findClass(name);
    } catch (ClassNotFoundException e) {
    c = super.loadClass(name);
    }
    }
    return c;
    }
    private byte[] loadClassData(String className) {
    byte[] buffer = new byte[1];
    String realName = className.replace('.', '/');
    realName += ".class";
    try {
    JarFile jarFile = new JarFile(urls[0].getFile());
    JarEntry entry = jarFile.getJarEntry(realName);
    if (entry != null) {
    buffer = new byte[(int) entry.getSize()];
    BufferedInputStream is = new BufferedInputStream(jarFile
    .getInputStream(entry));
    is.read(buffer);
    } else {
    buffer = null;
    }
    } catch (IOException e) {
    buffer = null;
    }
    return buffer;
    }
    }
     
    Frank Rouse, Jun 18, 2004
    #1
    1. Advertising

  2. Frank Rouse

    Roedy Green Guest

    On Fri, 18 Jun 2004 18:40:17 -0400, Frank Rouse <> wrote
    or quoted :

    > Object o = c.newInstance(); <===== Exception here.


    does the class have a no arg public constructor?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jun 18, 2004
    #2
    1. Advertising

  3. Frank Rouse

    Frank Rouse Guest

    Roedy Green wrote:

    > On Fri, 18 Jun 2004 18:40:17 -0400, Frank Rouse <> wrote
    > or quoted :
    >
    >
    >> Object o = c.newInstance(); <===== Exception here.

    >
    >
    > does the class have a no arg public constructor?
    >


    Thanks for the reply. I did create a no parameter constructor just
    for testing purposes. Still no luck. Open to other suggestions.

    Cheers
     
    Frank Rouse, Jun 19, 2004
    #3
  4. Frank Rouse

    Chris Smith Guest

    Frank Rouse wrote:
    > >> Object o = c.newInstance(); <===== Exception here.


    [...]

    > Thanks for the reply. I did create a no parameter constructor just
    > for testing purposes. Still no luck. Open to other suggestions.


    Frank,

    I missed your original post (my server has been acting up recently), but
    I don't see a mention of what exception you're getting. There are
    several exceptions that can be thrown by Class.newInstance(), and they
    indicate different problems.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jun 20, 2004
    #4
    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. amw
    Replies:
    1
    Views:
    3,036
    Chris Smith
    Sep 25, 2003
  2. mohamed
    Replies:
    1
    Views:
    12,048
  3. Karsten Wutzke

    JAR's inside a JAR + custom ClassLoader

    Karsten Wutzke, Jun 25, 2007, in forum: Java
    Replies:
    2
    Views:
    1,515
    Tom Hawtin
    Jun 25, 2007
  4. Andi
    Replies:
    11
    Views:
    2,797
    Roedy Green
    Mar 20, 2008
  5. tapas.adhikary
    Replies:
    1
    Views:
    1,476
    tapas.adhikary
    May 7, 2008
Loading...

Share This Page