简体   繁体   English

编译.java文件时找不到符号错误

[英]Cannot Find Symbol error when compiling .java file

Good day, I have two classes, Map and Field, in the same directory. 美好的一天,我在同一目录中有两个类Map和Field。 I successfully compiled Field.java but when i compile Map.java, i get this: 我成功地编译了Field.java,但是当我编译Map.java时,我得到了:

  Map.java:4: error: cannot find symbol
    private Field[][] gameMap;
            ^
  symbol:   class Field
  location: class Map
  Map.java:61: error: cannot find symbol
       public Field getFieldPosition(){
              ^
 symbol:   class Field
 location: class Map
 Map.java:8: error: cannot find symbol
                gameMap = new Field[10][20];
                              ^
 symbol:   class Field
 location: class Map
 3 errors

Here are the codes for the two classes: 这是两个类的代码:

public class Map {
private Field[][] gameMap;
private int row, col;
private int rowPlayer, colPlayer;
public Map(){//constructor
    gameMap = new Field[10][20];
    for(row=0;row<10;row++){
        for(col=0;col<20;col++){
            gameMap[row][col].setFieldUntilled();
        }
    }
    rowPlayer = 0;
    colPlayer = 0;
    gameMap[rowPlayer][colPlayer].setPlayerLoc();
}
public Field getFieldPosition(){
    return gameMap[rowPlayer][colPlayer];
}
}

and for Field.java(if necessary): 对于Field.java(如果需要):

public class Field {
private int playerLoc;
private char fieldType;
public Field(){//constructor
    fieldType = 'u';
    playerLoc = 0;
}
public void setFieldUntilled(){
    fieldType = 'u';
}
public void setFieldTilled(){
    fieldType = 't';
}
public void setPlayerLoc(){
    playerLoc = 1;
}
public void removePlayerLoc(){
    playerLoc = 0;
}
public int getPlayerLoc(){
    return playerLoc;
}
}

I compiled the Field.java and Map.java separately: javac Field.java did not return any errors but javac Map.java returned the errors above. 我分别编译了Field.java和Map.java:javac Field.java没有返回任何错误,但是javac Map.java返回了以上错误。

It's still unclear to me where exactly your problem is. 我仍然不清楚您的问题到底在哪里。 But I will tell you the steps to go with which you will be successfull. 但是,我将告诉您成功的步骤。

Hint: There will be many alternatives to what I now describe. 提示:我现在将描述许多替代方案。

I assumme that your source files for Map and Field do not have any package declarations and import statements. 我假设您的MapField源文件没有任何包声明和import语句。

You should have a separate project directory somewhere in your file system for your project. 在项目的文件系统中的某个位置,您应该有一个单独的项目目录。 Let's name that directory my-game . 让我们将该目录命名为my-game Additionally, you should have a source directory inside it. 此外,您应该在其中包含源目录。 Let's name it src . 我们将其命名为src You should place your source files into that source directory. 您应将源文件放入该源目录。 Your project directory layout now looks like: 您的项目目录布局现在看起来像:

my-game/
  |-- src/
       |-- Field.java
       |-- Map.java

If you want to compile the class Map with a simple command, you should be inside the src directory and call: 如果要使用简单的命令编译Map类,则应在src目录中并调用:

my-game/src> javac Map.java

This will result in both source files being compiled. 这将导致两个源文件都被编译。 The produced class files will also be put into that source directory. 产生的类文件也将放入该源目录中。

So far so good. 到现在为止还挺好。 It is better to be inside the project directory when compiling: 编译时最好放在项目目录中:

my-game> javac src/Map.java

But this will now lead to the compiler error you described (and to no class file being produced), as now the class Field is looked up in the wrong directory. 但这现在会导致您描述的编译器错误(并且不会生成任何类文件),因为现在在错误的目录中查找了Field类。 You need to tell the compiler where to look them up: 您需要告诉编译器在哪里查找它们:

my-game> javac -sourcepath src src/Map.java

This leads to the same result as before. 这导致了与以前相同的结果。

Even better now would be to separate the source and the target directory. 现在更好的是将源目录和目标目录分开。 First create a directory called bin inside your project directory, then compile: 首先在项目目录中创建一个名为bin目录,然后编译:

my-game> javac -sourcepath src -d bin src/Map.java

This will result in the following directory layout: 这将导致以下目录布局:

my-game/
  |-- src/
  |    |-- Field.java
  |    |-- Map.java
  |-- bin/
       |-- Field.class
       |-- Map.class

And if you have done the last step successfully, then use an IDE like Eclipse. 如果成功完成了最后一步,请使用Eclipse之类的IDE。 It has exactly this project directory layout, but you are not bothered with the exact command line for compiling. 它完全具有该项目目录的布局,但是您不必为编译的确切命令行而烦恼。

Almost without question, the problem is that you have the CLASSPATH environment variable set, and your value does not include ".", the current directory. 几乎没有问题,问题在于您已设置了CLASSPATH环境变量,并且您的值不包括当前目录“。”。 There are two different simple fixes for this problem: one would be to remove the environment variable, but that might break an installation of some third-party software that is depending on it. 有两个不同的简单修复程序解决此问题:一个是删除环境变量,但是这可能会中断某些依赖于该变量的第三方软件的安装。 The second way to fix this is just to use the –classpath argument as part of your compile line: 解决此问题的第二种方法是将–classpath参数用作编译行的一部分:

javac -classpath . Map.java 

好像您忘记了在Map类中导入Field一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM