• 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

my loader not working

 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i just tried extending java.lang.ClassLoader.
Then i tried loading the same class (TestLoader) using my loader as mentioned in javageeks.com
But the class loader that loaded TestLoader is getting used again.
TestLoader's findClass is'nt getting called at all!
I have the code here.
findClass just opens the .class file and reads in the bytes.
can comeone suggest something as to why the second tiem around my sutom class laoder did'nt get used.

regards
karthik.
import java.io.*;
public class TestLoader extends ClassLoader{
protected TestLoader(){
super(TestLoader.class.getClassLoader());
System.out.println("TestLoader");
}
public Class findClass(String name) throws ClassNotFoundException{
String file = "d:\\java\\ClassLoaders" + name + ".class";
byte[] classBytes = null;
Class retClass = null;
try{
FileInputStream fis = new FileInputStream(file);
classBytes = new byte[fis.available()];
fis.read(classBytes);
retClass= defineClass(name,classBytes,0,classBytes.length); }catch(Exception e){
e.printStackTrace();
throw new ClassNotFoundException("class not found");
}
return retClass;
}

public static void main(String[] args)throws Exception{
TestLoader loader = new TestLoader();
System.out.println(loader.getClass().getClassLoader());
TestLoader l = null;
Class c=loader.loadClass("TestLoader");
l = (TestLoader)c.newInstance();
System.out.println(l.getClass().getClassLoader());

}
}
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok this is what i found.
Since the TestLoader is in the same directory and in classpath, it got loaded by the parent loader.
if it's not the case then it seems our loader gets a chance to load the class?. I verified this.
can someone correct me??
am not able to verify the claim by ted nedward with this code!
is there a way to get our classloader to load a class present in the classpath?
karthik.
 
reply
    Bookmark Topic Watch Topic
  • New Topic