3

我有以下 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]
4

1 回答 1

1

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

于 2012-09-12T18:02:36.427 回答