Simple ClassLoader

J

jespersahner

Hi!

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

Regards,
Jesper
 
A

andysigner

Hi Jesper

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

Cheers
Andy

--------- 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
from
* loadClass above and from the internal function
FindClassFromClass.
*/
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,
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;
}
}

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

import java.io.FileInputStream;
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
FileInputStream("D:\\HelloWorld.class");
classData = new byte[fi.available()];
fi.read(classData);
}
catch(Exception e) {
System.out.println("Could not read file");
System.exit(1);
}

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

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

}
}
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top