PathsClassLoader extends ClassLoader { @Override protected Class findClass(String name) throws ClassNotFoundException { Class found = null; for (String aPath : classpath) { String bodyPath = name.replace('.', '/'); File pathFile = new File(aPath); if (pathFile.isDirectory()) { byte[] body; int bodySize; File bodyFile = new File(pathFile, bodyPath + ".class"); if (!bodyFile.exists()) { continue; } bodySize = (int) bodyFile.length(); body = new byte[bodySize]; FileInputStream bodyStream = null; try { bodyStream = new FileInputStream(bodyFile); int pos = 0; int len; do { len = bodyStream.read(body, pos, bodySize); pos += len; } while (pos < bodySize); } catch (IOException e) { throw new ClassNotFoundException("Class body not read", e); } finally { if (null != bodyStream) { try { bodyStream.close(); } catch (IOException ex) { } } } found = defineClass(name, body, 0, bodySize); break; } else if (pathFile.isFile()) { byte[] body; int bodySize; InputStream bodyStream = null; JarFile jar = null; try { jar = new JarFile(pathFile); JarEntry entry = jar.getJarEntry(bodyPath + ".class"); if (null == entry) { continue; } bodyStream = jar.getInputStream(entry); bodySize = (int) entry.getSize(); body = new byte[bodySize]; int pos = 0; int len; do { len = bodyStream.read(body, pos, bodySize); pos += len; } while (pos < bodySize); } catch (IOException e) { throw new ClassNotFoundException("Class body not read", e); } finally { if (null != bodyStream) { try { bodyStream.close(); } catch (IOException e) { } } if (null != jar) { try { jar.close(); } catch (IOException e) { } } } found = defineClass(name, body, 0, bodySize); break; } else { } } if (null == found) { throw new ClassNotFoundException(name); } else { resolveClass(found); return found; } } PathsClassLoader(ClassLoader parent, String[] classpath);   }
@Test public void testFindClass() throws Exception { System.out.println("findClass"); String[] classpath = { classFile.getParent(), jarFile.getCanonicalPath() }; PathsClassLoader instance = new PathsClassLoader(parentCL, classpath); Class result = instance.findClass(className); assertTrue("Should return a Class from file", result instanceof Class); classpath[0] = jarFile.getCanonicalPath(); instance = new PathsClassLoader(parentCL, classpath); result = instance.findClass(jarClassName); assertTrue("Should return a Class from JAR", result instanceof Class); }