Class.forName返回null

Tyl*_*ler 3 java classloader

我看过一篇关于Class.forName是否可以在这里返回null的帖子,每个人似乎认为它不能(或不会).但是,它使用以下代码为我返回null:

  public void init() {
    File binDriectory = new File("./bin/dft");
    String[] files = binDriectory.list();
    for (String file : files) {
      if (file.endsWith(".class")) {
        if (file.indexOf("DataReader") > 0) {
          //strip off the ".class"
          String className = file.substring(0, file.indexOf(".class"));

          try {
            //load the class
            Class readerclass = Class.forName("dft." + className);
            //get the file extension of the file type this class deals with

            /* NullPointerException thrown here in call to getMthod() */

            Method getExtensionMethod = readerClass.getMethod("getFileExtension", null);
            String extension = (String) getExtensionMethod.invoke(readerClass.newInstance(), null);
            //add the extension and class to the class map
            classMap.put(extension, readerClass);
            //add the extension to the list of reader file extensions
            readerExtensions.add(extension);
          }
          catch (ClassNotFoundException e) {
            System.err.println("**WARNING: class not found: dft." + className);
            continue;
          }
          catch (NoSuchMethodException e) {
            System.err.println("**WARNING: class dft." + className + " does "
                               + "not contain a getFileExtension() method.");
            continue;
          }
          catch (InstantiationException e) {
            System.err.println("**WARNING: could not create an instance of "
                               + "class dft." + className);
            continue;
          }
          /* with Java 7, these next two catch blocks can be combined (and they
             should) */
          catch (InvocationTargetException e) {
            System.err.println("**WARNING: could not invoke getFileExtension()"
                               + " method from class dft." + className);
            continue;
          }
          catch (IllegalAccessException e) {
            System.err.println("**WARNING: could not invoke getFileExtension()"
                + " method from class dft." + className);
            continue;
          }

          System.out.println(className);
        }
        else if (file.indexOf("DataWriter") > 0) {
          System.out.println(file);
        } 
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

不抛出ClassNotFoundException,但forName()的结果为null.文档没有说明返回null.

有没有人知道为什么会这样?我在另一个不使用包名的项目中测试了forName()的调用(上面的代码在名为"dft"的包中),并且一个工作正常.我认为这与它有关.类路径也很好 - .class文件在... bin/dft中,类路径包含.../bin.我甚至尝试在类路径中显式添加.../bin/dft目录以防万一,它仍然返回null.

use*_*421 9

要指定返回的值forName

        readerclass    (小写C)

但是getMethod来自

        readerClass    (大写C)

这可能是一个未初始化的领域.

Java区分大小写.