编译.java文件时找不到符号错误 [英] Cannot Find Symbol error when compiling .java file

查看:94
本文介绍了编译.java文件时找不到符号错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Good day,
我有两个类,Map和Field,在同一个目录中。我成功编译了Field.java,但是当我编译Map.java时,我得到:

Good day, I have two classes, Map and Field, in the same directory. I successfully compiled Field.java but when i compile Map.java, i get this:

  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


b $ b

这两个类的代码:

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];
}
}

/ p>

and for Field.java(if necessary):

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;
}
}






我分别编译了Field.java和Map.java:
javac Field.java没有返回任何错误,但是javac Map.java返回了上面的错误。


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.

推荐答案

我还不清楚你的问题在哪里。但我会告诉你你要成功的步骤。

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.

提示:我们现在描述的方法有很多种选择。

我假设您的源文件映射字段没有任何包声明和import语句。

I assumme that your source files for Map and Field do not have any package declarations and import statements.

您的项目文件系统中应该有一个单独的项目目录。让我们将该目录命名为 my-game 。另外,你应该有一个源目录。让我们将它命名为 src 。您应该将源文件放入该源目录。您的项目目录布局现在看起来像:

You should have a separate project directory somewhere in your file system for your project. Let's name that directory my-game. Additionally, you should have a source directory inside it. Let's name it 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



如果要编译class Map 用一个简单的命令,你应该在 src 目录中,并调用:

If you want to compile the class Map with a simple command, you should be inside the src directory and call:

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

但现在会导致你描述的编译器错误类文件正在产生),因为现在类 Field 在错误的目录中查找。你需要告诉编译器在哪里查找它们:

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. 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.

更好的办法是将源目录和目标目录分开。首先在项目目录中创建一个名为 bin 的目录,然后编译:

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:

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

如果你已经成功完成最后一步,那么使用类似Eclipse的IDE。它有这个项目目录布局,但你不打扰准确的命令行进行编译。

And if you have done the last step successfully, then use an IDE like Eclipse. It has exactly this project directory layout, but you are not bothered with the exact command line for compiling.

这篇关于编译.java文件时找不到符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆