0

Trying to learn Java again and I cannot remember how I figured this out the first time around.

I have 3 classes; a GameLauncher, GuessGame, and Player. GameLauncher has my main method.

They are all packaged as chap02, I cannot remember if that is important to me yet.

I am compiling like this: javac GameLauncher.java GuessGame.java Player.java

running like this: java GameLauncher

I am getting this error: Could not find or load main class GameLauncher.

I know this is a ridiculous issue, but I have always had trouble with this kind of stuff. The actual programming and writing code I can pick up just fine, but dealing with these damn compilers always gets me. Any help would be appreciated. Thanks

package chap02;

public class GameLauncher {
public static void main (String[] args) {
    GuessGame game = new GuessGame();
    game.startGame();
}
}

Edit: The issue isn't with the actual code, the issue is with how I am compiling it.

user2993636
  • 55
  • 1
  • 7
  • 1
    show us the code and the error – kirti Nov 05 '14 at 15:47
  • Using NetBeans or Eclipse is easier. Otherwise, execute `javac` from the parent dir. This could be helpful: http://stackoverflow.com/questions/8027670/compiling-four-java-files-within-one-package-using-javac – vefthym Nov 05 '14 at 15:53

1 Answers1

2

When running the program, you have to run it form the correct directory. Remember, that you used packages, so you have the following package-structure

  • src
    • chap02
      • GameLauncher.java
      • GuessGame.java
      • Player.java

after compilation, you will find the corresponding .class-files in the chap02-folder. To start the game, you have run the following command from the src-directory:

java chap02.GameLauncher

Since you specified a package in the source code, you have to use the full qualified name, including the package, to which the class belongs.

EDIT as vefthym mentioned, you have to compile the code in the same way, running

javac chap02/GameLauncher.java

from the source-directory.

EDIT 2 "src" is the directory, where your src lies. I, for example, have my Code unter X:\JDK-Projects[Project-name]\src. You have to specify the full absolute path to the src-Directory or the relative path form the directory you are currently in.

Turing85
  • 13,364
  • 5
  • 27
  • 49
  • Does src mean the drive that my folder is in? Currently the folder is in my c: drive, so would I do it like this from the command prompt? ||| cd c:\chap02 ||| javac chap02*.java ||| java chap02.GameLauncher ||| ? – user2993636 Nov 05 '14 at 16:06
  • If all of you files are located under C:\chap02\src, then you should chdir to C:\chap02 and execute from there: cd C:\chap02 && javac chap02.GameLauncher.java && java chap02.GameLauncher – Turing85 Nov 05 '14 at 17:11