// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class BytesToClass {
    private static byte[] load(String fnm) throws IOException {
        File f = new File(fnm);
        byte[] b = new byte[(int)f.length()]; 
        InputStream is = new FileInputStream(f);
        int ix = 0;
        while(ix < b.length) {
            ix += is.read(b, ix, b.length - ix);
        }
        is.close();
        return b;
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        byte[] b = load("C:\\X.class");
        SpecialClassLoader scl = new SpecialClassLoader();
        scl.add("test.X", b);
        Object o = Class.forName("test.X", true, scl).newInstance();
        System.out.println(o);
    }
}

class SpecialClassLoader extends ClassLoader {
    private Map<String, byte[]> m = new HashMap<String,byte[]>();
    public void add(String clznam, byte[] code) {
        m.put(clznam, code);
    }
    protected Class<?> findClass(String name) {
        byte[] bc = m.get(name);
        return defineClass(name, bc, 0, bc.length);
    }
}
