我有以下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);
}
}
这个类实现了一个自定义Comparator,它与这个类位于同一个项目中,但是这个类加载器没有拾取它,并且在加载类时会爆炸。如果我将以下行添加到类
,它不会给出任何错误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);
}
}
如果有多个文件,我希望它循环加载多个文件
编辑: -verbose输出
[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]
答案 0 :(得分:1)
如果在运行时将-verbose
作为参数添加到JVM,该怎么办?你会看到类加载细节。