Java编译错误找不到接口符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19070172/
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-12 13:52:08  来源:igfitidea点击:

Java compilation error cannot find symbol of interface

javaclassinheritanceinterfacecompiler-errors

提问by blaughli

I am trying to implement an interface in my java code as such:

我正在尝试在我的 java 代码中实现一个接口,如下所示:

package PJ1;

public class Fraction implements FractionInterface, Comparable<Fraction>{

Now, FractionInterface.class in the same directory as the Fraction.java file, and it is also in package PJ1:

现在, FractionInterface.class 与 Fraction.java 文件位于同一目录中,并且也在包 PJ1 中:

package PJ1;

public interface FractionInterface{

Yet when I try to compile my Fraction.java file, I get the following error:

然而,当我尝试编译 Fraction.java 文件时,出现以下错误:

D:\CSC220\PJ1\Fraction.java:36: error: cannot find symbol
public class Fraction implements FractionInterface, Comparable<Fraction>
                                 ^

I'm stumped, since all of my related files are in the same directory and I'm trying to put all of my class files in the same package. Any ideas?

我很难过,因为我所有的相关文件都在同一个目录中,而且我试图将所有类文件放在同一个包中。有任何想法吗?

采纳答案by Reimeus

My guess is that the files are not in a directory called PJ1relative to where the compiler expects them to be. Create the folder and move both files to that location. To make it a bit clearer, let's say your folder structure looks like this

我的猜测是这些文件不在PJ1与编译器期望它们所在的位置相关的目录中。创建文件夹并将两个文件移动到该位置。为了更清楚一点,假设您的文件夹结构如下所示

myfolder
 +-PJ1
    Fraction.java
    FractionInterface.java

Then you need to be compiling from myfolderusing

然后你需要从myfolder使用编译

javac PJ1\Fraction.java

回答by santu

try to compile like this:

尝试这样编译:

e.g. in c: you do have both the java files - Fraction.java and FractionInterface.java , and you have not created any folder for packages yet, then try as:

例如在 c: 你有两个 java 文件 - Fraction.java 和 FractionInterface.java ,你还没有为包创建任何文件夹,然后尝试:

c:> javac -d . *.java

This will compile all the files with creating required packages. You no need to create any folders for packages manually.

这将编译所有文件并创建所需的包。您无需手动为包创建任何文件夹。

If you already have created the folder for packages, and you are already in the package say:

如果您已经为包创建了文件夹,并且您已经在包中,请说:

c:\PJ1, you can simply compile using javac as:

c:\PJ1,你可以简单地使用 javac 编译为:

c:\PJ1> javac *.java

Hope this will work.

希望这会奏效。

回答by prisco.napoli

Be sure that both files are in PJ1 folder and run javac *.java.

确保两个文件都在 PJ1 文件夹中并运行javac *.java.

回答by Ain-e Muhammad

Just go to a directory above the directory in which your files exist and compile your java files from there (so that you can see if they compile correctly), e.g.

只需转到您的文件所在目录上方的目录并从那里编译您的java文件(以便您可以查看它们是否正确编译),例如

javac dir1\file.java.

javac dir1\file.java。