Java “找不到符号”编译错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3903330/
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
"Cannot Find Symbol" compile error
提问by Jay
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.
我写了两个接口:Class 和 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?
我的游戏类已编译,类文件与两个 java 文件位于同一目录中。我一直无法找到解决方案。有没有人有任何想法?
回答by akf
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.
类名区分大小写。您可能创建了一个名为 的接口game
,但您在Class
接口声明中将Game
其称为,而编译器找不到该接口。
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:
但是,您还有另一种可能是从Impl
包中进行编译。为此,您需要引用类路径,以便编译器可以从包结构的基础中找到类。您可以在类名之前添加一个-classpath ..
arg javac
:
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:
或者,您可以执行更常见的操作,从包结构的根目录进行编译。为此,您需要指定Class
文件的路径:
javac Impl\Class.java
you can always add a -classpath .
to be clear.
你可以随时添加一个-classpath .
来清楚。
回答by Stephen C
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".
您需要阅读 Java 类路径的工作原理以及应该如何组织源代码。基本上,你的问题是当javac编译器编译“Class.java”时,它不希望在当前目录中找到“Game.class”。它(可能)正在“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.
IBM“管理 Java 类路径”页面深入讨论了如何设置类路径以及 Java 实用程序(例如java
和javac
)如何使用它来查找类文件。Oracle“设置类路径”页面更简洁地提供了更多信息……但您需要仔细阅读。
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 calledjava.lang.Class
which is imported by default.
- Java 包名称应全部小写。
- 调用一个类
Class
是一个坏主意,因为这会与java.lang.Class
默认导入的被调用的类发生冲突。