【发布时间】:2012-09-05 16:55:48
【问题描述】:
我有以下 CustomClassLoader
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Java;
/**
*
* @author noconnor
*/
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import org.apache.commons.io.FilenameUtils;
public class CustomClassLoader extends ClassLoader {
private static String repoLocation = "C:/TempBINfolder/bin/";
public CustomClassLoader() {
}
public CustomClassLoader(ClassLoader parent) {
super(parent);
}
@Override
protected Class<?> findClass(final String name)
throws ClassNotFoundException {
AccessControlContext acc = AccessController.getContext();
try {
return (Class) AccessController.doPrivileged(
new PrivilegedExceptionAction() {
public Object run() throws ClassNotFoundException {
FileInputStream fi = null;
try {
String fileName = FilenameUtils.getBaseName(name);
String path = FilenameUtils.getFullPath(name);
setRepoLocation(path);
fi = new FileInputStream(getRepoLocation() + fileName + ".class");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int read;
while ((read = fi.read(buffer)) > 0) {
baos.write(buffer, 0, read);
}
byte[] classBytes = baos.toByteArray();
return defineClass(fileName, classBytes, 0,
classBytes.length);
} catch (Exception e) {
throw new ClassNotFoundException(name);
}
}
}, acc);
} catch (java.security.PrivilegedActionException pae) {
return super.findClass(name);
}
}
public String getRepoLocation() {
return repoLocation;
}
public void setRepoLocation(String repoLocation) {
this.repoLocation = repoLocation;
}
}
它适用于大多数课程,我遇到了一个问题。我有以下java类
public class IntegerComparator
implements Comparator<Integer>
{
public IntegerComparator(){}
public int compare(Integer a, Integer b)
{ int aValue = a.intValue();
int bValue = b.intValue();
return (aValue - bValue);
}
}
这个类实现了一个自定义的比较器,它与这个类位于同一个项目中,但它不会被类加载器拾取,并且会在加载类时被炸掉。它没有给出任何错误,但是如果我将以下行添加到类中
import java.util.Comparator;
CustomClassLoader 有效。这让我相信我缺少文件的类路径或类似的东西。有人知道怎么解决吗?
编辑:这是调用类加载器的代码
Class stringClass = null;
for (String file : classFiles) {
ClassLoader cls = new CustomClassLoader(ClassLoader.getSystemClassLoader());
try {
stringClass = cls.loadClass(file);
tempList.add(stringClass);
} catch (ClassNotFoundException ex) {
Logger.getLogger(CompilerForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
如果有多个文件,我希望它循环加载多个文件
编辑: -详细输出
[Loaded java.net.MalformedURLException from C:\Program Files\Java\jdk1.7.0_05\jre\lib\rt.jar]
[Loaded Java.CustomClassLoader$1 from file:/C:/projects/Compiler/Compiler/build/classes/]
[Loaded java.lang.ClassFormatError from C:\Program Files\Java\jdk1.7.0_05\jre\lib\rt.jar]
[Loaded org.junit.runners.model.MultipleFailureException from file:/C:/projects/Compiler/Compiler/Jar%20Files/junit-4.11-SNAPSHOT-20120416-1530.jar]
[Loaded org.junit.runner.notification.Failure from file:/C:/projects/Compiler/Compiler/Jar%20Files/junit-4.11-SNAPSHOT-20120416-1530.jar]
[Loaded java.io.StringWriter from C:\Program Files\Java\jdk1.7.0_05\jre\lib\rt.jar]
[Loaded org.junit.runner.notification.RunNotifier$4 from file:/C:/projects/Compiler/Compiler/Jar%20Files/junit-4.11-SNAPSHOT-20120416-1530.jar]
[Loaded org.junit.runner.notification.RunNotifier$7 from file:/C:/projects/Compiler/Compiler/Jar%20Files/junit-4.11-SNAPSHOT-20120416-1530.jar]
[Loaded org.junit.runner.notification.RunNotifier$2 from file:/C:/projects/Compiler/Compiler/Jar%20Files/junit-4.11-SNAPSHOT-20120416-1530.jar]
【问题讨论】:
-
“Bombs out”到底是什么意思?虽然很高兴看到类加载器代码,但使用它的代码在哪里?
-
当我尝试加载类(在调试时)它只是直接移出方法,这很奇怪,因为它没有给出错误消息,但如上所述,如果我添加导入语句,它将成功加载类
-
添加了调用CustomClassLoader的代码
标签: java class import classpath