0

While working on a project, I tried to compile my java file in Terminal, but I got the error "cannot find symbol". I usually compile and run my code in Eclipse's IDE so this is my first time using Terminal. The errors seem to appear when I try to create instances of a class written in the same folder.

MainPanel.java:30: error: cannot find symbol
    static Player p1 = new Player( 30, 245, 0 );
           ^
  symbol:   class Player
  location: class MainPanel

How could I solve this problem? Thanks!

Trevor Aquino
  • 11
  • 1
  • 5
  • You are not compiling correctly. Show the full `javac` and location of all relevant files. Make sure you are calling it from the root directory of your source files – Kon Apr 17 '15 at 20:34
  • What is the classpath that you are using? – Michal Kordas Apr 17 '15 at 20:34
  • @Kon `Trevors-MacBook-Pro:main TrevorAquino$ javac MainPanel.java` this is how I tried to compile the program, with my Player class also inside of the main folder. @MichalKordas how am I able to tell Terminal what classpath I am using? – Trevor Aquino Apr 17 '15 at 20:44

1 Answers1

4

When your Java project gets to be more than 3 files and you use external libraries, it's best to let your IDE or even a build tool such as Maven (https://maven.apache.org/) handle compilation for you. I suggest you switch to those now instead of manually compiling.

But for now, try compiling the Player first then MainPanel classes like so

javac Player.java MainPanel.java

or when you have multiple classes depending on each other, this should work also

javac *.java

If your curious what the problem is, it is that your MainPanel class uses the Player class and thus the Java compiler needs to have compiled and be aware of the Player class first before it can compile MainPanel. But you probably did not yet compile the Player class so the compiler threw that error.

11th Hour Worker
  • 337
  • 3
  • 14
  • I'm facing the same problem if I compile one by one, but after using ``javac *.java`` they both get compiled but still get the error that **wrong name ClassName** the ClassName is the one that contains the main method. – Kennerdol Aug 29 '22 at 18:59