You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

链表代码调试求助:编译通过后遇找不到符号及主类加载错误

问题排查与解决

错误信息

mnt/ro/voccontent1/gradinglib/ccc_v1_c_48ao_206651/asn281740_9/
asn281741_1/asnlib.0/LinkedListTests.java:64: error: cannot find sym
bol                                                                 
            assertEquals("Julia", ( (LinkedList) list2).getTail().ge
tData(), "The tail shjould point at the node with data \"Julia\" ");
                                                                 ^  
      symbol:   method getData()                                     
      location: class Object                                         
    1 error                                                          
    Error: Could not find or load main class RunTest                
    Caused by: java.lang.ClassNotFoundException: RunTest 

第一个错误:找不到getData()方法

你的LinkedList类中,getTail()方法返回的是T类型(即节点存储的数据本身),而非Node对象。看你代码里的实现:

public T getTail(){
    return this.tail.data;
}

但测试代码却在getTail()之后调用了.getData()——相当于给一个普通数据对象(比如字符串"Julia")调用getData(),这类对象本身没有这个方法,所以触发报错。

解决办法二选一:

  1. 修改测试代码,删除多余的.getData():
assertEquals("Julia", ((LinkedList) list2).getTail(), "The tail should point at the node with data \"Julia\" ");
  1. 若你原本设计getTail()是返回Node对象,就修改LinkedList的getTail()实现:
public Node getTail(){
    return this.tail;
}

注意:Node是私有内部类,测试代码可能无法直接访问,因此更推荐第一种方案。

第二个错误:找不到RunTest主类

常见原因及解决方向:

  • 编译命令未覆盖RunTest.java,导致没有生成RunTest.class文件,检查编译指令是否完整
  • 运行时的工作目录不对,或未指定正确的类路径,导致JVM找不到编译后的类文件
  • RunTest类的文件名和类名不一致,确认文件名是RunTest.java,且类定义为public class RunTest

内容的提问来源于stack exchange,提问作者Mike W

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 12:12:33