“找不到符号”编译错误
我的编码经验只有几年,所以这个问题应该很容易回答。
我写了两个接口: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类名区分大小写。您可能创建了一个名为
game
的接口,但您在Class
接口声明中将其引用为Game
,编译器无法这样做寻找。但是,还有另一种可能是您从
Impl
包中进行编译。为此,您需要引用类路径,以便编译器可以从包结构的基础中找到类。您可以在类名之前将-classpath ..
arg 添加到javac
中:或者,您可以执行更常见的操作,从包结构的根进行编译。为此,您需要指定
Class
文件的路径:您始终可以添加
-classpath .
以保持清晰。Class names are case sensitive. It is possible that you have created an interface called
game
, but you refer to it in yourClass
interface declaration asGame
, 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 yourjavac
before the class name: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:you can always add a
-classpath .
to be clear.您需要阅读 Java 类路径的工作原理以及应如何组织源代码。基本上,你的问题是当javac编译器编译“Class.java”时,它不希望在当前目录中找到“Game.class”。它(可能)正在“Impl/Game.class”中寻找它。
IBM“管理 Java 类路径”页面提供了深入的讨论如何设置类路径以及 java 实用程序(例如
java
和javac
)如何使用它来查找类文件。 Oracle“设置类路径”页面提供了更多信息更简洁...但您需要仔细阅读。顺便说一句,您的代码中有一些风格上的错误:
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
andjavac
) 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:
Class
is a bad idea, because this collides with the class calledjava.lang.Class
which is imported by default.