• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to run this code?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are these three files of  a guessing game but something seems to be incomplete as when i try to run it it gives me an error like this:

GameLauncher.java:5: error: cannot find symbol
       GuessGame game = new GuessGame();
       ^
 symbol:   class GuessGame
 location: class GameLauncher
GameLauncher.java:5: error: cannot find symbol
       GuessGame game = new GuessGame();
                            ^
 symbol:   class GuessGame
 location: class GameLauncher
2 errors

Here is the code for the "GameLauncher.java":-



"Player.java"



"GuessGame.java"



Here is the location of all the files on my storage:-

"C:\Users\Manish\Desktop\head-first-java(examples)\9780596009205-master\chap02\GameLauncher.java"

"C:\Users\Manish\Desktop\head-first-java(examples)\9780596009205-master\chap02\GuessGame.java"

"C:\Users\Manish\Desktop\head-first-java(examples)\9780596009205-master\chap02\Player.java"

Hope it helps!
 
Greenhorn
Posts: 12
IntelliJ IDE Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have run these codes on the same package on IDE, but I did not get such an error. Have you tried on IDE?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you compiling the classes (what command)? How are you running them? What is the directory you compile/run them from? (type cd in the console).
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wholehartedly agree with Ulvi. An IDE takes away all those command line horrors. But unfortunately, sometimes you have no choice.

Your files are in a package called "chap02". That meeans that GameLauncher will look for a file "chap02\GuessGame.class".
Suppose that you have a directory structure (like I have): d:\javaranch\chap02, and that your 3 java files are in the map chap02.
Now, make sure you are in the map javaranch, then compile GameLauncher with the command:

javac chap02\GameLauncher.java

(note the backslash!)

When that succeeds, you can now run GameLauncher with the command (also from the map javaranch): java chap02/GameLauncher

But take note: I am using Windows 10 powershell, and for half an hour I got exactly the same error as you had. I used the command

java chap02\GameLauncher

using the backslash, as always on windows. But it only worked when I changed the backslash into a forward slash:

java chap02/GameLauncher

And to quote the four Yorkshiremen: tell this to the youth, and they won't believe you
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:java chap02\GameLauncher

using the backslash, as always on windows. But it only worked when I changed the backslash into a forward slash:

java chap02/GameLauncher


I'd have expected it to be ran like:
java chap02.GameLauncher <-- I'd say that is a habitual way to execute class when it resides in a package. That works.

I'm surprised it let's execute the way you showed (indeed it works, I tried myself for my curiosity).
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:
I'd have expected it to be ran like:
java chap02.GameLauncher <-- I'd say that is a habitual way to execute class when it resides in a package. That works.

I'm surprised it let's execute the way you showed (indeed it works, I tried myself for my curiosity).


You are right. Last time I practised this was when I prepared for the OCAJP...  long time ago.    
 
Manish Pamnani
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulvi Mardaliyev wrote:I have run these codes on the same package on IDE, but I did not get such an error. Have you tried on IDE?



I haven't tried IDE but i am trying on Command Line
 
Manish Pamnani
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:How are you compiling the classes (what command)? How are you running them? What is the directory you compile/run them from? (type cd in the console).



C:\Users\XXXX\Desktop>javac GameLauncher.java
javac: file not found: GameLauncher.java
Usage: javac <options> <source files>
use -help for a list of possible options

C:\Users\XXXX\Desktop>javac GuessGame.java
GuessGame.java:84: error: class GameLauncher is public, should be declared in a file named GameLauncher.java
public class GameLauncher {
      ^
1 error

This is the path directory and the command i am using to compile my source file
 
Manish Pamnani
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:I wholehartedly agree with Ulvi. An IDE takes away all those command line horrors. But unfortunately, sometimes you have no choice.

Your files are in a package called "chap02". That meeans that GameLauncher will look for a file "chap02\GuessGame.class".
Suppose that you have a directory structure (like I have): d:\javaranch\chap02, and that your 3 java files are in the map chap02.
Now, make sure you are in the map javaranch, then compile GameLauncher with the command:

javac chap02\GameLauncher.java

(note the backslash!)

When that succeeds, you can now run GameLauncher with the command (also from the map javaranch): java chap02/GameLauncher

But take note: I am using Windows 10 powershell, and for half an hour I got exactly the same error as you had. I used the command

java chap02\GameLauncher

using the backslash, as always on windows. But it only worked when I changed the backslash into a forward slash:

java chap02/GameLauncher

And to quote the four Yorkshiremen: tell this to the youth, and they won't believe you



I tried what you said and here are the results:-

javac chap02\GameLauncher.java
chap02\GameLauncher.java:5: error: cannot access GuessGame
       GuessGame game = new GuessGame();
       ^
 bad class file: .\chap02\GuessGame.class
   class file contains wrong class: chap2.GuessGame
   Please remove or make sure it appears in the correct subdirectory of the classpath.
1 error
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chaos. Slow down and try to understand how things work first.

Directories where the files are it seems keep changing every time you are trying to compile them, so the instructions what people suggest getting out of sync with your directories set up.

1. Without making things over-complicated for a start, can you create a directory called "test" on your C drive.
2. Can you place 3 files inside the directory created during the step 1.

Tell when you done.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you done what's in post above.

3. Navigate to C:\test directory

4. Execute instruction javac -d . GameLauncher.java GuessGame.java Player.java
Note: -d option means create directory structure specified as package in a current working directory which is denoted by . (dot).

5. Issue instruction java chap02.GameLauncher
 
Manish Pamnani
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Once you done what's in post above.

3. Navigate to C:\test directory

4. Execute instruction javac -d . GameLauncher.java GuessGame.java Player.java
Note: -d option means create directory structure specified as package in a current working directory which is denoted by . (dot).

5. Issue instruction java chap02.GameLauncher



I just did what you said, here are the results:-

C:\test>javac -d . GameLauncher.java GuessGame.java Player.java

C:\test>java chap02.GameLauncher
I'm thinking of a number between 0 and 9...
Number to guess is 1
I'm guessing 4
I'm guessing 7
I'm guessing 4
Player one guessed 4
Player two guessed 7
Player three guessed 4
Players will have to try again.
Number to guess is 1
I'm guessing 8
I'm guessing 4
I'm guessing 7
Player one guessed 8
Player two guessed 4
Player three guessed 7
Players will have to try again.
Number to guess is 1
I'm guessing 2
I'm guessing 4
I'm guessing 2
Player one guessed 2
Player two guessed 4
Player three guessed 2
Players will have to try again.
Number to guess is 1
I'm guessing 5
I'm guessing 0
I'm guessing 9
Player one guessed 5
Player two guessed 0
Player three guessed 9
Players will have to try again.
Number to guess is 1
I'm guessing 1
I'm guessing 2
I'm guessing 1
Player one guessed 1
Player two guessed 2
Player three guessed 1
We have a winner!
Player one got it right? true
Player two got it right? false
Player three got it right? true
Game is over
 
Manish Pamnani
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Once you done what's in post above.

3. Navigate to C:\test directory

4. Execute instruction javac -d . GameLauncher.java GuessGame.java Player.java
Note: -d option means create directory structure specified as package in a current working directory which is denoted by . (dot).

5. Issue instruction java chap02.GameLauncher



Liutauras Vilda

Thanks for helping me out, i was wondering if i was gonna get the answer but you helped a newbie like me.
Thanks again!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic