gpt4 book ai didi

Java:在 CustomClassLoader 中添加类路径

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:12:07 27 4
gpt4 key购买 nike

我有以下 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;
}
}

它适用于大多数类(class),但我遇到了一个问题。我有以下 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]

最佳答案

如果在运行时将-verbose作为参数添加到JVM 中会怎么样。您会看到类加载的详细信息。

关于Java:在 CustomClassLoader 中添加类路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12392106/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com