【问题标题】:Class not found Exception when creating an object array创建对象数组时找不到类异常
【发布时间】:2013-12-30 18:42:37
【问题描述】:

所以,我写了这些类:

public class FicherosDeCiudadanos {

   public static int numCiudadanos (File f) {
       try{

           Scanner texto=new Scanner(f);
           int contador=0;
           while(texto.hasNextLine()){
               contador++;
               texto.nextLine();
           }
           texto.close();
           return contador;
       }
   }

   public static  Ciudadano[] leerFichero (File f) {
      try{
          Scanner texto=new Scanner(f);
          //This next line throws classNotFound when debbugging but only 
          //when evaluating Ciudadano[]
          Ciudadano[] tablaCiudadano = new Ciudadano[numCiudadanos(f)];
          ....    
          //TO-DO
   }
}

我验证它有效,并且确实有效,leerFichero() 方法完美地完成了这项工作。它使用 TO-DO 中的代码创建了一个 Ciudadano 的对象数组。但是,在另一个包中,我调用方法leerFichero(),它到达那里,计算出numCiudadanos() 来设置数组的长度,但是当它到达Ciudadano[] 时,它抛出ClassNotFoundException,然后程序停止。当我从它自己的包中使用它时它仍然有效。

我猜这是堆栈

Thread [main] (Suspended)   
owns: Object  (id=36)   
owns: Object  (id=37)   
ClassNotFoundException(Throwable).<init>(String, Throwable) line: 286   
ClassNotFoundException(Exception).<init>(String, Throwable) line: not available 
ClassNotFoundException(ReflectiveOperationException).<init>(String, Throwable) line: not available  
ClassNotFoundException.<init>(String) line: not available   
URLClassLoader$1.run() line: not available  
URLClassLoader$1.run() line: not available  
AccessController.doPrivileged(PrivilegedExceptionAction<T>, AccessControlContext) line: not available [native method]   
Launcher$ExtClassLoader(URLClassLoader).findClass(String) line: not available   
Launcher$ExtClassLoader(ClassLoader).loadClass(String, boolean) line: not available 
Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: not available 
Launcher$AppClassLoader.loadClass(String, boolean) line: not available  
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: not available  
FicherosDeCiudadanos.leerFichero(File) line: 54 
Operaciones.nombreCliente(int) line: 80 
Operaciones.listadoCompras() line: 37   
TrabajoProg1.ejecutarOperacion(int) line: 40    
TrabajoProg1.main(String[]) line: 22    

【问题讨论】:

  • 发布异常的堆栈跟踪。
  • 请验证调用者(代码调用“leerFichero”)是否在其类路径中正确导入类 Ciudadano。

标签: java classnotfoundexception


【解决方案1】:

这可能是构建路径问题。您需要右键单击外部包并转到

构建路径 > 配置构建路径

并导航到

项目

然后,单击Add... 并添加包含FicherosDeCiudadanos 的项目。

确保在调用类的顶部有一行:

import package1.FicherosDeCiudadanos

其中package1 是包含FicherosDeCiudadanos 的包

【讨论】:

  • 两个包都在同一个项目中。并且构建路径包含了项目中的所有包
  • leerFichero() 中的第 54 行是什么?
  • Ciudadano[] tablaCiudadano = new Ciudadano[numCiudadanos(f)];
  • Ciudadano 和 FicherosDeCiudadanos 类在同一个包中,并且仍然导入到调用方法 leerFichero() 的类中。对不起,如果我很难理解
  • 确保 Ciudadano 的原始文件仍然可以从 Eclipse 内部访问。可能是您的工作区“忘记”了它的位置,需要重新导入。
【解决方案2】:

有时调试器与某些类不同步。

尝试清理/构建项目,甚至重新启动 Eclipse(或其他 IDE)。然后再次尝试调试。

【讨论】:

    【解决方案3】:

    所以,最后那个错误没有任何意义,后来又出现了另一个错误,这似乎让它停止了。

    【讨论】:

      猜你喜欢
      • 2017-10-20
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      • 2018-04-02
      • 2021-12-09
      • 2015-03-20
      相关资源
      最近更新 更多