JVM_八股&场景题

时间:2025-03-10 17:07:53
import java.io.*; public class MyClassLoader extends ClassLoader { @Override protected Class<?> findClass(String name) throws ClassNotFoundException { byte[] data = loadClassData(name); return defineClass(name, data, 0, data.length); } private byte[] loadClassData(String name) { try { File file = new File(name.replace(".", "/") + ".class"); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int b; while ((b = fis.read()) != -1) { baos.write(b); } fis.close(); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } } public static void main(String[] args) throws Exception { MyClassLoader loader = new MyClassLoader(); Class<?> clazz = loader.loadClass("com.example.MyClass"); System.out.println("Class loaded: " + clazz.getName()); } }