“找不到符号”编译错误

发布于 2024-09-27 05:48:15 字数 516 浏览 0 评论 0原文

我的编码经验只有几年,所以这个问题应该很容易回答。

我写了两个接口:Class 和 Game。接口CLASS应该扩展接口GAME。

以下是两个接口源代码:

package Impl;

public interface Game
{
    //METHODS AND VARS
}


package Impl;    

public interface Class extends Game
{
    //METHODS AND VARS
}

现在,当我尝试编译第二个接口时,出现以下错误:

class.java:4: cannot find symbol
symbol: class Game
public interface Class extends Game
                               ^

我的游戏类已编译,并且类文件与两个 java 文件位于同一目录中。我一直无法找到解决方案。有人有什么想法吗?

My coding experience has only gone back a few years, so this question should be easy enough to answer.

I have written two interfaces: Class and Game. Interface CLASS is supposed to extend interface GAME.

Here are the two interface sources:

package Impl;

public interface Game
{
    //METHODS AND VARS
}


package Impl;    

public interface Class extends Game
{
    //METHODS AND VARS
}

Now, when I try to compile the second interface, I get the following error

class.java:4: cannot find symbol
symbol: class Game
public interface Class extends Game
                               ^

My Game class is compiled and the class file is in the same directory as both java files. I have not been able to find a solution. Does anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨敛清荷 2024-10-04 05:48:15

类名区分大小写。您可能创建了一个名为 game 的接口,但您在 Class 接口声明中将其引用为 Game,编译器无法这样做寻找。

但是,还有另一种可能是您从 Impl 包中进行编译。为此,您需要引用类路径,以便编译器可以从包结构的基础中找到类。您可以在类名之前将 -classpath .. arg 添加到 javac 中:

javac -classpath .. Class.java

或者,您可以执行更常见的操作,从包结构的根进行编译。为此,您需要指定 Class 文件的路径:

javac Impl\Class.java

您始终可以添加 -classpath . 以保持清晰。

Class names are case sensitive. It is possible that you have created an interface called game, but you refer to it in your Class interface declaration as Game, which the compiler cannot find.

However, there is another chance that you are compiling from within your Impl package. To do this, you will need to reference your classpath such that the compiler can find classes from the base of the package structure. You can add a -classpath .. arg to your javac before the class name:

javac -classpath .. Class.java

Alternatively, you can do what is more common, compiling from the root of your package structure. To do so, you will need to specify the path to your Class file:

javac Impl\Class.java

you can always add a -classpath . to be clear.

っ左 2024-10-04 05:48:15

您需要阅读 Java 类路径的工作原理以及应如何组织源代码。基本上,你的问题是当javac编译器编译“Class.java”时,它不希望在当前目录中找到“Game.class”。它(可能)正在“Impl/Game.class”中寻找它。

IBM“管理 Java 类路径”页面提供了深入的讨论如何设置类路径以及 java 实用程序(例如 javajavac)如何使用它来查找类文件。 Oracle“设置类路径”页面提供了更多信息更简洁...但您需要仔细阅读。

顺便说一句,您的代码中有一些风格上的错误:

  • Java 包名称应该全部小写。
  • 调用类 Class 是一个坏主意,因为这与默认导入的名为 java.lang.Class 的类发生冲突。

You need to read up on how Java classpaths work and how you should organize your source code. Basically, your problem is that when javac compiler compiles "Class.java", it does not expect to find "Game.class" in the current directory. It (probably) is looking for it in "Impl/Game.class".

The IBM "Managing the Java classpath" page provides an in-depth discussion of how to set your classpath and how java utilities (e.g. java and javac) use it to find class files. The Oracle "Setting the Classpath" page provides more information more succinctly ... but you need to read it carefully.

By the way, you've got some style atrocities in your code:

  • Java package names should be in all lower-case.
  • Calling a class Class is a bad idea, because this collides with the class called java.lang.Class which is imported by default.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文