• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

UserDefine ClassLoader

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not that I am aware of.
 
reply
    Bookmark Topic Watch Topic
  • New Topic