Discussions

General J2EE: Custom ClassLoader not working

  1. Custom ClassLoader not working (2 messages)

    I have written a custom classoader and trying to load some classes but I am getting exception. My ClassLoader Class: package com.self.classloading; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; public class CustomClassLoader extends ClassLoader { private static final String dir = "D:\\classes
    "; public CustomClassLoader() { super(); } public CustomClassLoader(ClassLoader parent) { super(parent); } @Override protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException { System.out.println("0 : "+name); Class c = findLoadedClass(name); if (c == null) { try { System.out.println("1 : "+name); c = findClass(name); } catch (ClassNotFoundException e) { System.out.println("2 : "+name); } } if (c == null) { System.out.println("3 : "+name); getParent().loadClass(name); } if (resolve) { resolveClass(c); } return c; } @Override protected Class findClass(String name) throws ClassNotFoundException { byte[] b = loadClassBytes(name); return defineClass(name, b, 0, b.length); } private byte[] loadClassBytes(String name) throws ClassNotFoundException { byte[] bytes = null; try { String path = name.replace(".", "
    ").concat(".class"); File file = new File(dir + path); FileInputStream fin = new FileInputStream(file); BufferedInputStream bin = new BufferedInputStream(fin); bytes = new byte[(int) file.length()]; while (bin.read(bytes) != -1) { } } catch (Exception e) { throw new ClassNotFoundException(e.getMessage()); } return bytes; } } I have two classes , ClassA and ClassB(which extends ClassA). My ClassLoader test class: package com.self.basics; import java.net.URL; import java.net.URLClassLoader; import java.util.HashMap; import com.self.classloading.CustomClassLoader; public class TestClasses { public static void main(String[] args)throws Exception { Class clz1=new CustomClassLoader().loadClass("com.self.basics.ClassB"); } }

    Threaded Messages (2)

  2. The Exception that I am getting[ Go to top ]

    0 : com.self.basics.ClassB 1 : com.self.basics.ClassB 0 : com.self.basics.ClassA 1 : com.self.basics.ClassA 0 : java.lang.Object 1 : java.lang.Object 2 : java.lang.Object 3 : java.lang.Object Exception in thread "main" java.lang.NoClassDefFoundError: java/lang/Object at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.lang.ClassLoader.defineClass(Unknown Source) at com.self.classloading.CustomClassLoader.findClass(CustomClassLoader.java:49) at com.self.classloading.CustomClassLoader.loadClass(CustomClassLoader.java:30) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.lang.ClassLoader.defineClass(Unknown Source) at com.self.classloading.CustomClassLoader.findClass(CustomClassLoader.java:49) at com.self.classloading.CustomClassLoader.loadClass(CustomClassLoader.java:30) at java.lang.ClassLoader.loadClass(Unknown Source) at com.self.basics.TestClasses.main(TestClasses.java:16)
  3. Re: Custom ClassLoader not working[ Go to top ]

    I am so curious on why you are writing code that does this?