【问题标题】:Java: Add classpath the CustomClassLoaderJava:将类路径添加到 CustomClassLoader
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 您知道如何在 Netbeans 中执行此操作吗?我知道如何在 eclipse 中执行此操作,但我不知道如何在 Netbeans 中执行此操作?
  • 尝试如下:点击项目->属性->选择运行->VM选项是你的行
  • +1 表示 -verbose:但我之前从未使用过 -verbose,我在输出从 customclassloader 移出后直接添加了输出,知道这个输出发生了什么吗?跨度>
  • 它实际上显示了从什么位置加载了什么类。可以在此处找到详细信息和其他参数,例如docs.oracle.com/javase/6/docs/technotes/tools/windows/java.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-22
  • 1970-01-01
相关资源
最近更新 更多