Java 接口实现错误:找不到符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24523764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:44:54  来源:igfitidea点击:

Interface Implementation error : cannot find symbol

javainterface

提问by misguided

I am implementing the following sample interface:

我正在实现以下示例界面:

package test1;
    public interface MotorVehicle {
        void run();    
        int getFuel();
    }

In the class

在课堂里

package test1;
import test1.MotorVehicle;
public class Car implements MotorVehicle
{
    int fuel;

    public void run(){
        System.out.println("Running");
    }
    public int getFuel(){
        return this.fuel;
    }
}

When I try to compile the class file , I get the following error :

当我尝试编译类文件时,出现以下错误:

Car.java:4: error: cannot find symbol
public class Car implements MotorVehicle
                            ^
  symbol: class MotorVehicle
1 error

Compile Steps:

编译步骤:

Step:1javac MotorVehicle.javaStep:2javac Car.java

步骤:1javac MotorVehicle.java步骤:2javac Car.java

Both my interface and the class are in the same directory , why does ut come up with cannot find symbol error?

我的界面和类都在同一个目录下,为什么会出现找不到符号错误?

Edit: As suggested , have changed the package , and tried to run the same code again . Still getting an error.

编辑:按照建议,已更改包,并尝试再次运行相同的代码。仍然出现错误。

enter image description here

在此处输入图片说明

采纳答案by blueygh2

The problem is that you're in the wrong folder when compiling. From the console screenshot, it is clear that you are inside /test1. However, the package test1;statement expects a folder inside the current folder named test1. It can't find that folder/package, so you get an error.

问题是编译时您位于错误的文件夹中。从控制台截图,很明显你在里面/test1。但是,该package test1;语句需要在当前文件夹内有一个名为test1. 它找不到那个文件夹/包,所以你得到一个错误。

The solution is to go up one folder, so you end up in /src, then compile using the path to the file, e.g. javac test1/Car.java. Explanation: You are in the folder /src, the packagestatement inside the classes says they are inside the folder test1which is inside /src. Now every package/path can be resolved.

解决方案是进入一个文件夹,因此您最终进入 . /src,然后使用文件的路径进行编译,例如javac test1/Car.java. 说明:你在文件夹中/srcpackage类里面的语句说它们在文件夹里面,test1里面是/src。现在可以解析每个包/路径。

And you shouldn't importthings that are in the same package.

而且你不应该把import东西放在同一个包里。

回答by shazin

First compile MotorVehicleas it doesn't have any dependencies. Then set the classpath

首先编译,MotorVehicle因为它没有任何依赖项。然后设置类路径

Before issuing javac Car.javacompile statements you need to set the Classpath

在发出javac Car.java编译语句之前,您需要设置Classpath

Windows

视窗

set CLASSPATH=%CLASSPATH%;<PATH_TO_COMPILED_BINARY>/

Unix

Unix

export CLASSPATH=$CLASSPATH:<PATH_TO_COMPILED_BINARY>/

<PATH_TO_COMPILED_BINARY>should not include the package test1

<PATH_TO_COMPILED_BINARY>不应该包括包裹 test1

Example :

例子 :

C:/sourcecode/test1

C:/源代码/测试1

Then <PATH_TO_COMPILED_BINARY>should be C:/sourcecode

那么<PATH_TO_COMPILED_BINARY>应该是 C:/sourcecode

Update

更新

Removing the import test1.MotorVehiclewill also fix the issue.

删除import test1.MotorVehicle也将解决该问题。

回答by SparkOn

First of all as your package name is testyou must keep your class and the interface in a folder named test.

首先,因为您的包名称是 test您必须将类和接口保存在名为test.

Second thing since they are in the same folder named testremove import test.MotorVehicle;from the class defination

第二件事,因为它们位于从类定义中test删除的同一个文件夹中import test.MotorVehicle;

Suppose if your folder testresides in g:/such that g:/test/contains class and the interface.

假设您的文件夹test位于g:/g:/test/contains 类和接口中。

Then try opening the command prompt in g:/

然后尝试打开命令提示符 g:/

then type the following commands

然后输入以下命令

for compiling

编译用

javac test/Car.java

and for executing

并执行

 java test.Car

Though you may get Error: Main method not found in class test.Caras your class does not contain main mathod

虽然您可能会Error: Main method not found in class test.Car因为您的课程不包含主要数学

回答by CoderCroc

You are going in to exact path by the use of cdcommand.Because of that interface is not accessible as class will try to find out it from package from current/running location.

您将通过使用cd命令进入确切路径。因为该接口不可访问,因为类将尝试从当前/运行位置的包中找到它。

For make this compile you have to specify fully (again Fully)qualified name of package during compilation.

要进行此编译,您必须在编译期间指定完全(再次完全)限定的包名称。

For Example

例如

If you class is in a.b.testpackagecompile it like this

如果您的课程在a.b.test包中,请像这样编译

javac a/b/test/Car.java