public class CompilingClassLoader extends ClassLoader{ // Given a filename, read the entirety of that file from disk // and return it as a byte array. private byte[] getBytes( String filename ) throws IOException { // Find out the length of the file File file = new File( filename ); long len = file.length();

// Create an array that's just the right size for the file's // contents byte raw[] = new byte[(int)len];

// Open the file FileInputStream fin = new FileInputStream( file );

// Read all of it into the array; if we don't get all, // then it's an error. int r = fin.read( raw ); if (r != len) throw new IOException( "Can't read all, "+r+" != "+len );

// First, see if we want to try compiling. We do if (a) there // is source code, and either (b0) there is no object code, // or (b1) there is object code, but it's older than the source if (javaFile.exists() && (!classFile.exists() || javaFile.lastModified() > classFile.lastModified())) {

// Another place where we might come to if we fail // to compile throw new ClassNotFoundException( ie.toString() ); } }

// Let's try to load up the raw bytes, assuming they were // properly compiled, or didn't need to be compiled try {

// read the bytes byte raw[] = getBytes( classFilename );

// try to turn them into a class clas = defineClass( name, raw, 0, raw.length ); } catch( IOException ie ) { // This is not a failure! If we reach here, it might // mean that we are dealing with a class in a library, // such as java.lang.Object }

//System.out.println( "defineClass: "+clas );

// Maybe the class is in a library -- try loading // the normal way if (clas==null) { clas = findSystemClass( name ); }

//System.out.println( "findSystemClass: "+clas );

// Resolve the class, if any, but only if the "resolve" // flag is set to true if (resolve && clas != null) resolveClass( clas );

// If we still don't have a class, it's an error if (clas == null) throw new ClassNotFoundException( name );