Simple ClassLoader

Discussion in 'Java' started by jespersahner, Mar 6, 2005.

  1. jespersahner

    jespersahner Guest


    Say that I have a string of bytes (byte[]) defining a class, how do I
    create a simple ClassLoader to define the class?

    jespersahner, Mar 6, 2005
    1. Advertisements

  2. jespersahner

    andysigner Guest

    Hi Jesper

    A small far from perfect example how to write your own classloader.
    Have a closer look at Classloader class provided by Sun.


    --------- Classloader ---------

    package net.n0fx.util;

    import java.util.Hashtable;

    public class ByteClassloader extends ClassLoader {
    private Hashtable classes = new Hashtable();

    * This is a simple version for external clients since they will
    always want
    * the class resolved before it is returned to them.
    public Class loadClass(String className) throws
    ClassNotFoundException {
    return (loadClass(className, true));

    * This is the required version of loadClass which is called both
    * loadClass above and from the internal function
    public synchronized Class loadClass(String className, boolean
    resolveIt) throws ClassNotFoundException {
    return loadClass(className, null, resolveIt);

    public Class loadClass(String className, byte[] classData, boolean
    resolveIt) throws ClassNotFoundException{
    Class result;

    /* Check our local cache of classes */
    result = (Class)classes.get(className);
    if(result != null) {
    return result;

    /* Check with the primordial class loader */
    try {
    result = super.findSystemClass(className);
    return result;
    catch(ClassNotFoundException e) {
    System.out.println(" >>>>>> Not a system class.");

    /* Time to check if byte[] is not null */
    if(classData == null) {
    throw new ClassNotFoundException();

    /* Define it (parse the class file) */
    result = defineClass(className, classData, 0,
    if(result == null) {
    throw new ClassFormatError();

    if(resolveIt) {

    classes.put(className, result);
    System.out.println(" >>>>>> Returning newly loaded
    return result;

    --------- Test ---------
    package net.n0fx.util;

    import java.lang.reflect.InvocationTargetException;

    import org.diediedie.util.ByteClassloader;

    public class TestByteClassloader {

    public static void main(String[] args) {
    byte[] classData = null;
    try {
    FileInputStream fi = new
    classData = new byte[fi.available()];;
    catch(Exception e) {
    System.out.println("Could not read file");

    Class c;
    ByteClassloader bc = new ByteClassloader();
    try {
    // Load class
    c = bc.loadClass("net/n0fx/test/HelloWorld", classData,

    //Invoke main methode
    String[] arguments = null;
    Class params[] = { String[].class };
    c.getDeclaredMethod("main", params).invoke(null, new
    Object[] { null });
    catch(Exception e) {

    andysigner, Mar 8, 2005
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.