• Post Reply Bookmark Topic Watch Topic
  • New Topic

UserDefine ClassLoader  RSS feed

 
Joybrata Chakraborty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have written one class loader which will load a class, now if I want use that class , ClassNotFoundException is thorwn.


Here is the code:


MyClassLoader classLoader = new MyClassLoader();
try {
Class c = Class.forName("Abc", true, classLoader);
System.out.println(c.getName());

Object o = c.newInstance();
System.out.println(o);

Class a = Class.forName("Abc");
o=a.newInstance();
System.out.println(o);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Output:
Abc
Abc [i=0, str=null]
java.lang.ClassNotFoundException: Abc
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at classloader.TestClassLoader.main(TestClassLoader.java:36)


Please help on this.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class a = Class.forName("Abc"); will use the system class loader. This class loader does not know anything about your MyClassLoader instance or the classes it has loaded. (Your MyClassLoader instance does know about the system class loader though, since that's its "parent".)
The system class loader will try to load class "Abc" using its own mechanism. It can't find it, so it throws an exception.

Your MyClassLoader instance is doing its work just fine, as both Class c and the object created by it are printed out without any problems.
 
Joybrata Chakraborty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Class a = Class.forName("Abc"); will use the system class loader. This class loader does not know anything about your MyClassLoader instance or the classes it has loaded. (Your MyClassLoader instance does know about the system class loader though, since that's its "parent".)
The system class loader will try to load class "Abc" using its own mechanism. It can't find it, so it throws an exception.

Your MyClassLoader instance is doing its work just fine, as both Class c and the object created by it are printed out without any problems.



So is there any way in java , where if that class is loaded by any(Custom) classloader then, system classloader will not try to load it again.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not that I am aware of.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!