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
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):
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.
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.
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
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
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.
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
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.
Almost without question, the problem is that you have the CLASSPATH environment variable set, and your value does not include ".", the current directory. 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:
javac -classpath . Map.java
Looks like you forgot to import Field in your Map class.