Dynamically load laf



Hello everyone!
I am writing an application and I would like to give the user the
option of choosing Look and Feel.
The look and feels that are part of standard java is not that exciting
so I would like to give the user the option of "installing" new look
and feels without recompile or anything.
My idea was to have a directory say lib/laf where the user would put a
the look and feel with the full name as filename say
"net.sourceforge.mlf.metouia.MetouiaLookAndFeel.jar"
Thus I would add that directory to my classpath and all would be
peaches. Well not quite. It seems this is not really working. So I
wrote my own classloader trying to solve this problem. But my
classLoader does not seem to work. (code included last) so i wonder if
anyone has any good ideas on how to solve this. Not neccerily using a
custom classloader, any other ideas are welcome.

regards
Daniel

ps. I searched for this on both google and deja, but got no good hits,
so maybe my keywords were poorly chosen, so if anyone has any websites
dealing with this problem i appreciate links.

code

public class LaFClassLoader
extends ClassLoader {
public LaFClassLoader() {
}
public Class findClass(String name) throws ClassNotFoundException {
File dir= new File("lib/LaF");
if(dir.exists()){
File[] lafs= dir.listFiles();
for(int i=0;i<lafs.length;i++){
if (lafs[i].getName().endsWith(".jar")) {
try{
ZipInputStream zin = new ZipInputStream(new
FileInputStream(lafs[i]));
ZipEntry entry;
while ( (entry = zin.getNextEntry()) != null) {
if (entry.isDirectory()) {
continue; // since this entry is just a directory
}
String s=entry.getName();
String tname=name.replace('.','/')+".class";
if (entry.getName().equals(tname)) {
long size = entry.getSize();
if (size != -1) {
byte[] clas = new byte[ (int) size];
zin.read(clas, 0, clas.length);
return super.defineClass(name, clas, 0, clas.length);
}
}

}
}catch(Exception e){
throw new ClassNotFoundException(name,e);
}
}
}

}

throw new ClassNotFoundException(name);

}



}

problem I get with this classloader is on the super.defineClass line
saying
"net/sourceforge/mlf/metouia/MetouiaLookAndFeel" contains invalid
UTF-8 character.

.



Relevant Pages

  • Re: empty interfaces via reflection
    ... Have a custom class loader since the system ... MyClass ... public class MyClassLoader extends ClassLoader ...
    (comp.lang.java.programmer)
  • UnsatisfiedLinkError using Java Webstart with custom classloader
    ... have a custom classloader which will be used for some classes. ... public class ApplicationStarter ... NativeInterface nativeInterface = new NativeInterface; ... this.loadedClasses.put(name, clazz); ...
    (comp.lang.java.programmer)
  • Re: JDK5 Generics
    ... by hardwiring the choice of classloader you are making it unusable for ... containers and the like) load all application classes via custom classloaders, ... then you should have the code that creates the Pool pass the class object ... public class Pool ...
    (comp.lang.java.programmer)
  • Re: empty interfaces via reflection
    ... Looking back at an earlier post, your custom MyClassLoader goes like this: ... "When requested to find a class or resource, a ClassLoader instance will ... public class MyClassLoader extends ClassLoader ... private int foo; ...
    (comp.lang.java.programmer)