// 1. is this class already loaded? Class cls = findLoadedClass(className); if (cls != null) { return cls; }
// 2. get class file name from class name String clsFile = className.replace('.', '/') + ".class";
// 3. get bytes for class byte[] classBytes = null; try { InputStream in = getResourceAsStream(clsFile); byte[] buffer = new byte[BUFFER_SIZE]; ByteArrayOutputStream out = new ByteArrayOutputStream(); int n = -1; while ((n = in.read(buffer, 0, BUFFER_SIZE)) != -1) { out.write(buffer, 0, n); } classBytes = out.toByteArray(); } catch (IOException e) { log("ERROR loading class file: " + e); }
if (classBytes == null) { throw new ClassNotFoundException("Cannot load class: " + className); }
// 4. turn the byte array into a Class try { cls = defineClass(className, classBytes, 0, classBytes.length); if (resolve) { resolveClass(cls); } } catch (SecurityException e) { // loading core java classes such as java.lang.String // is prohibited, throws java.lang.SecurityException. // delegate to parent if not allowed to load class cls = super.loadClass(className, resolve); }
if (Base64.class.equals(clsB64)) { System.out.println("Base64 loaded through custom loader is the same as that loaded by System loader."); } else { System.out.println("Base64 loaded through custom loader is NOT same as that loaded by System loader."); }
// call the main method in Base64 java.lang.reflect.Method main = clsB64.getMethod("main", new Class[] {String[].class}); main.invoke(null, new Object[]{ new String[]{} }); } }