Cannot Find Symbol error when compiling .java file

前端 未结 3 1730
刺人心
刺人心 2021-01-27 02:32

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: e         


        
相关标签:
3条回答
  • 2021-01-27 02:39

    Looks like you forgot to import Field in your Map class.

    0 讨论(0)
  • 2021-01-27 02:54

    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 
    
    0 讨论(0)
  • 2021-01-27 03:02

    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.

    0 讨论(0)
提交回复
热议问题