5

I'm trying to call the Game class (that resides in src/main/java/Game.java) from within the Main class (that resides in src/main/java/Main.java), but I'm getting this error:

Main.java:3: error: cannot find symbol Game hangman = new Game(); symbol: class Game; location: class Main::: Main.java:3: error: cannot find symbol Game hangman = new Game(); symbol: class Game; location: class Main::: 2 errors; error: compilation failed

Here is the code that is in the Main.java file:

public class Main {
  public static void main (String[] args) {
    Game hangman = new Game();
    hangman.initialize();
  }
}

Here is the code that is in the Game.java file:

public class Game {
  private String randomWord;
  private int maxGuesses;
  private int guessCount;

  public Game () {
    this.maxGuesses = 8;
    this.guessCount = 0;
  }

  public void incrementGuessCount () {
    this.guessCount++;
  }

  public String generateRandomWord () {
    String[] words = {"puppy", "pool", "avalanche"};
    String result = words[(int) Math.floor(Math.random() * words.length)];
    this.randomWord = result;
    return result;
  }

  public void initialize () {
    String word = this.generateRandomWord();
    System.out.println(word);
  }
}

I'm using WSL, and this is my current Java version.

openjdk 11.0.13 2021-10-19 OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.20.04) OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)

  • 1
    How are you compiling your code? – tgdavies Feb 10 '22 at 06:50
  • Are you using packages? – Scary Wombat Feb 10 '22 at 06:51
  • 2
    The "classic" answer (prior to Java 9 [modules](https://learnjava.co.in/what-is-the-difference-between-a-module-and-a-package/)) is to organize your Java classes in packages, which are in turn organized by subdirectory. That's probably a good solution here: just use packages. – paulsm4 Feb 10 '22 at 06:52
  • @tgdavies thanks for asking. I simply ran the Main.java file using "java Main.java" command from terminal. I'm new to Java, so bear with me- hehe. – goldwzrd666 Feb 10 '22 at 06:55
  • You need to compile the code first using javac – Scary Wombat Feb 10 '22 at 06:56
  • @ScaryWombat I'm not using packages, but can learn to make one for these classes so that they're compatible. I just figured I shouldn't have to since they're in the same directory. – goldwzrd666 Feb 10 '22 at 06:56
  • 2
    Running a `.java` file directly with `java` only works for single source file programs. You need to compile first with `javac Main.java Game.java`. Then you can run with `java Main`. – tgdavies Feb 10 '22 at 06:58
  • @paulsm4 I'll get accustomed to packages! I appreciate the feedback. – goldwzrd666 Feb 10 '22 at 06:59
  • @tgdavies Nice! This is getting me a new error- which is always good. I'll get back to this asap once I work through this new error. – goldwzrd666 Feb 10 '22 at 07:03
  • @tgdavies I'm not sure how to make your comment the answer, but it is the answer. First, I had to update my "javac" so that it's the same version as my "java". Second, I had to compile my java files using "javac Main.java Game.java". Third, I had to run the code using "java Main" (NOT "java Main.java"). Here are some links that helped me: https://stackoverflow.com/questions/57359396/how-to-fix-error-class-found-on-application-class-path-main-in-java-for-visu https://stackoverflow.com/questions/17609083/update-alternatives-warning-etc-alternatives-java-is-dangling – goldwzrd666 Feb 10 '22 at 07:20
  • 1
    Here are some other links that would have helped you: https://stackoverflow.com/questions/18093928 and https://stackoverflow.com/questions/25706216 – Stephen C Feb 10 '22 at 07:25

0 Answers0