0

我一直在使用 CustomerClassLoader,因为我试图将 .class 文件加载到其中JUnitCore.runClasses(...);,但出现以下错误

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: SimpleTest (wrong name: JUnit/SimpleTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at Java.CustomClassLoader.access$000(CustomClassLoader.java:16)
at Java.CustomClassLoader$1.run(CustomClassLoader.java:45)
at java.security.AccessController.doPrivileged(Native Method)
at Java.CustomClassLoader.findClass(CustomClassLoader.java:33)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
.....

我不知道它出了什么问题,因为我传递了正确的类名和类的路径。这是我正在使用的代码

自定义类加载器

import java.io.FileInputStream;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;

public class CustomClassLoader extends ClassLoader {

String repoLocation = "C:/TempBINfolder/bin/JUnit/";

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 path = name.replace('.', '/');
    fi = new FileInputStream(repoLocation + path
      + ".class");
    byte[] classBytes = new byte[fi.available()];
    fi.read(classBytes);
    return defineClass(name, classBytes, 0,
      classBytes.length);
   }catch(Exception e )
   {
    throw new ClassNotFoundException(name);
   }
  }
 }, acc);
} catch (java.security.PrivilegedActionException pae) {
return super.findClass(name);
}
}
}

这就是我所说的

ClassLoader cls= new CustomClassLoader(ClassLoader.getSystemClassLoader());
Class stringClass = null;
    try {
        stringClass = cls.loadClass("SimpleTest");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(CompilerForm.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        stringClass.newInstance();
    } catch (InstantiationException ex) {
        Logger.getLogger(CompilerForm.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(CompilerForm.class.getName()).log(Level.SEVERE, null, ex);
    }

    JUnitCore.runClasses(stringClass);

有谁知道我为什么会收到这个错误?

4

4 回答 4

1

改变

stringClass = cls.loadClass("SimpleTest");

stringClass = cls.loadClass("JUnit.SimpleTest");

(即添加类文件的全限定包名)

String repoLocation = "C:/TempBINfolder/bin/JUnit/";

String repoLocation = "C:/TempBINfolder/bin/";

于 2012-08-15T18:04:35.390 回答
0

您必须将您的 SimpleTest 类声明为在 JUnit 包中,但您尝试加载的只是 SimpleTest(无包)。尝试一下:

  1. 删除包声明
  2. 从位置删除 Junit:String repoLocation = "C:/TempBINfolder/bin/";
于 2012-08-15T16:42:27.587 回答
0

类名应该是完全限定的junit.samples.SimpleTest

    stringClass = cls.loadClass("SimpleTest");

应该

    stringClass = cls.loadClass("junit.sample.SimpleTest");
于 2012-08-15T17:06:55.277 回答
-1

有时这会发生在我身上,通常可以通过清理和构建来解决。如果这不起作用,我通常会重新启动我的 IDE。

于 2012-08-15T16:40:34.920 回答