Java在同一目录中找不到.class的符号 [英] Java can't find symbol from .class in the same directory

查看:326
本文介绍了Java在同一目录中找不到.class的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件:MyStringLog.java(它扩展了ArrayStringLog)和TestDriver.java在同一目录。为什么TestDriver.java不能从MyStringLog类中识别MyStringLog构造函数?在TestDriver.java中我声明:

  MyStringLog animals = new MyStringLog(animals); 

这是为了构造一个名为animals的新MyStringLog对象,




解决方案

我得到这个大致工作。要这样做...



为文件 ArrayStringLog.java ,我删除了实现StringLogInterface ,因为我根本没有访问该接口。这样做后,我删除了包ch02.stringLogs; 。当我编译这个文件与包... 仍然在那里,我收到错误:线程中的异常mainjava.lang.NoClassDefFoundError: ArrayStringLog(错误的名称:ch02 / stringLogs / ArrayStringLog)



然后在 MyStringLog.java ,我删除了 import ch02.stringLogs。*; 。然后我保存并编译代码,并运行TestDriver,我没有收到编译错误。



这让我相信你的错误源于 ArrayStringLog中的语句。 java






为了最终得到一个编译,我把所有四个文件(ArrayStringLog,MyStringLog ,StringLogInterface,TestDriver)放入同一目录,删除任何包... 语句,添加回 implements StringLogInterface 到ArrayStringLog.java,编译每一个,然后使用添加的 toString 方法运行TestDriver,输出为:

 日志:animals 

1. dog
2. cat
pre>

这里是测试驱动程序:

  public class TestDriver { 
public static void main(String [] args){
MyStringLog animals = new MyStringLog(animals);
animals.insert(dog);
animals.insert(cat);

System.out.println(animals.toString());
}
}

清楚起见,ArrayStringLog开头是:

  public class ArrayStringLog implements StringLogInterface 


I have two files: MyStringLog.java (which extends ArrayStringLog) and TestDriver.java in the same directory. Why doesn't TestDriver.java recognize the MyStringLog constructor from the MyStringLog class? In TestDriver.java I declare:

MyStringLog animals = new MyStringLog("animals");

This is supposed to construct a new MyStringLog object named animals, but I get 2 errors when I compile, both, that MyStringLog symbol is not found.

解决方案

I got this to roughly work. To do so...

for the file ArrayStringLog.java, I removed the implements StringLogInterface because I simply don't have access to that interface. After doing so, I removed the package ch02.stringLogs;. When I compiled this file with the package... still there, I recieved the error: Exception in thread "main" java.lang.NoClassDefFoundError: ArrayStringLog (wrong name: ch02/stringLogs/ArrayStringLog)

Then in MyStringLog.java, I removed the import ch02.stringLogs.*;. I then saved and compiled the code, and ran the TestDriver, to which I received no compilation errors.

This leads me to believe that your error stems from the package statement in ArrayStringLog.java.


To finally get a compilation, I put all four files (ArrayStringLog, MyStringLog, StringLogInterface, TestDriver) into the same directory, removed any package... statements, added back implements StringLogInterface to ArrayStringLog.java, compiled each one, and then ran TestDriver with an added toString method from which the output was:

Log: animals

1. dog
2. cat

Here was the test driver:

public class TestDriver {
    public static void main(String[] args) {
        MyStringLog animals = new MyStringLog("animals");
        animals.insert("dog");
        animals.insert("cat");

        System.out.println(animals.toString());
    }
}

To make clear, ArrayStringLog begins with:

public class ArrayStringLog implements StringLogInterface

这篇关于Java在同一目录中找不到.class的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆