public class DyLoader extends ClassLoader
{
public DyLoader()
{
super(DyLoader.class.getClassLoader());
} public Class loadFromCustomRepository(String className) { String classPath = System.getProperty("java.class.path");
System.out.println("\n"+classPath); List classRepository = new ArrayList(); if ( (classPath != null) && ! (classPath.equals(""))) {
StringTokenizer tokenizer = new StringTokenizer(classPath,
File.pathSeparator);
while (tokenizer.hasMoreTokens()) {
classRepository.add(tokenizer.nextToken());
}
}
Iterator dirs = classRepository.iterator();
byte[] classBytes = null; while (dirs.hasNext()) {
String dir = (String) dirs.next();
//replace '.' in the class name with File.separatorChar & append .class to the name
String classFileName = className.replace('.', File.separatorChar);
classFileName += ".class";
try {
File file = new File(dir + File.separatorChar + classFileName);
if (file.exists()) {
InputStream is = new FileInputStream(file);
classBytes = new byte[is.available()];
is.read(classBytes);
break;
}
}
catch (IOException ex) {
System.out.println("IOException raised while reading class file data");
ex.printStackTrace();
return null;
}
}
return this.defineClass(className, classBytes, 0, classBytes.length);//シモヤリタ
}}
这个是重写的类加载器
用的时候总是报java.lang.NullPointerException
{
public DyLoader()
{
super(DyLoader.class.getClassLoader());
} public Class loadFromCustomRepository(String className) { String classPath = System.getProperty("java.class.path");
System.out.println("\n"+classPath); List classRepository = new ArrayList(); if ( (classPath != null) && ! (classPath.equals(""))) {
StringTokenizer tokenizer = new StringTokenizer(classPath,
File.pathSeparator);
while (tokenizer.hasMoreTokens()) {
classRepository.add(tokenizer.nextToken());
}
}
Iterator dirs = classRepository.iterator();
byte[] classBytes = null; while (dirs.hasNext()) {
String dir = (String) dirs.next();
//replace '.' in the class name with File.separatorChar & append .class to the name
String classFileName = className.replace('.', File.separatorChar);
classFileName += ".class";
try {
File file = new File(dir + File.separatorChar + classFileName);
if (file.exists()) {
InputStream is = new FileInputStream(file);
classBytes = new byte[is.available()];
is.read(classBytes);
break;
}
}
catch (IOException ex) {
System.out.println("IOException raised while reading class file data");
ex.printStackTrace();
return null;
}
}
return this.defineClass(className, classBytes, 0, classBytes.length);//シモヤリタ
}}
这个是重写的类加载器
用的时候总是报java.lang.NullPointerException
try
{
DyLoader loader=new DyLoader();
t=(temp)(loader.loadFromCustomRepository("temp").newInstance());}
catch(Exception ex)
{
System.out.println(ex.toString());
}
使用时产生的
E:\Borland\EJA_Test\classes\temp.class
E:\Borland\JBuilder2006\jdk1.5\sample\jnlp\servlet\jnlp-servlet.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\sample\jnlp\servlet\jnlp.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\sample\jnlp\servlet\jardiff.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\lib\dt.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\lib\jconsole.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\lib\htmlconverter.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\lib\tools.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\demo\jfc\Java2D\Java2Demo.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\demo\jfc\CodePointIM\CodePointIM.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\demo\plugin\jfc\CodePointIM\CodePointIM.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\demo\plugin\jfc\Java2D\Java2Demo.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\ext\dnsns.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\ext\localedata.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\ext\sunpkcs11.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\ext\sunjce_provider.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\deploy.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\plugin.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\javaws.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\jsse.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\rt.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\jce.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\im\indicim.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\im\thaiim.jar\temp.class
E:\Borland\JBuilder2006\jdk1.5\jre\lib\charsets.jar\temp.class
这么多路径肯定有问题
笨是要有限度的!
把你的程序改为:
public class DyLoader extends ClassLoader
{
public DyLoader()
{
super(DyLoader.class.getClassLoader());
}public Class loadFromCustomRepository(String className) {String classPath = System.getProperty("java.class.path");
System.out.println("\n"+classPath);List classRepository = new ArrayList();if ( (classPath != null) && ! (classPath.equals(""))) {
StringTokenizer tokenizer = new StringTokenizer(classPath,
File.pathSeparator);
while (tokenizer.hasMoreTokens()) {
classRepository.add(tokenizer.nextToken());
}
}
Iterator dirs = classRepository.iterator();
byte[] classBytes = null;while (dirs.hasNext()) {
String dir = (String) dirs.next();
//replace '.' in the class name with File.separatorChar & append .class to the name
String classFileName = className.replace('.', File.separatorChar);
classFileName += ".class";
try {
System.out.println("我是笨蛋: "+dir + File.separatorChar + classFileName);
File file = new File(dir + File.separatorChar + classFileName);
if (file.exists()) {
InputStream is = new FileInputStream(file);
classBytes = new byte[is.available()];
is.read(classBytes);
break;
}
}
catch (IOException ex) {
System.out.println("IOException raised while reading class file data");
ex.printStackTrace();
return null;
}
}
return this.defineClass(className, classBytes, 0, classBytes.length);//シモヤリタ
}}然后再调用一次,然后把输出的内容贴上来
temp t=null;
DyLoader loader=new DyLoader();
t=(temp)loader.loadFromCustomRepository("eja_test.temp").newInstance();
类型转换时的问题把,难道不应该转换类型吗