• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Really, really new to Java. Trying to compile small program, help!

 
Anthony Schmitt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really fresh off the boat, just a forewarning. i spent two semesters on Perl, and I know some HTML and CSS, but have very little programming experience.

Can't upload zips, argh

OK, here's the code from the 3 java files. if there's a better way of doing this someone please let me know!
























I'm working out of the "Head First Java, SE" text. Some of you may be aware of it, as it's been reviewed on this site. Well, there's a program in Chapter two called GuessGame. It generates a random number, then three players take turns guessing numbers until someone's guess matches the random integer.

javac GuessGame.java runs with no errors, but when I type java GameLauncher.java I get:


Owner-Owners-MacBook:Code OwnerOwner$ javac GameLauncher.java
GameLauncher.java:4: cannot find symbol
symbol : method startGame()
location: class GuessGame
game.startGame();
^
1 error



What am I missing here?

Thanks in advance! I know I'll have may questions along the way. i only hope somewhere down the line I'll have learned enough to answer someone else's questions!
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure such a method and class exisit.

Also when tyou compile a class,make sure all the needed classes are present in the classpath.

FYI

Visit this link for further information

webpage
 
Henry Wong
author
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony Schmitt wrote:What am I missing here?

Thanks in advance! I know I'll have may questions along the way. i only hope somewhere down the line I'll have learned enough to answer someone else's questions!


Java is case sensitive. You named the method in your class, the startgame() method. However, you used the method as the startGame() method. Notice the capital G, which denotes a completely different method.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added code tags to make your source much more legible.
 
Anthony Schmitt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new enough that I'm not sure what a classpath is, and terms like class and method are still new to me. I found my lowercase g in the first set of code and changed it to startGame().


Now, I'm getting this:

Owner-Owners-MacBook:Code OwnerOwner$ java GameLauncher
Exception in thread "main" java.lang.NoClassDefFoundError: GameLauncher
Caused by: java.lang.ClassNotFoundException: GameLauncher
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:254)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:399)


That means when I compiled GuessGame.java it didn't make a class for GameLauncher. Why?
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the class you are trying to compile uses some other class and it is not able to find those classes during compilation.

To set the classpath

Visit the link i posted
 
Anthony Schmitt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will do that. I'm just a bit worried about taking too much in too fast. That page has a disclaimer! A bit scary for a greenhorn such as myself.
 
Anthony Schmitt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I attempted to make sense of it but really it's too much information for someone as new to this as I am.

Why would the textbook not explain classpaths if I needed to understand them before writing the above code?
 
Anthony Schmitt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I need to put some code into one of these three java files to tell java where to look for things like Math.random?


Really need my hand held here, this is the first time I've ever attempted something like this.
 
Anthony Schmitt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, someone sent me a PM recommending I package my three java files. I've never done a package before, so I had to look it up. Please let me know if I did this right...














Strangely enough, now that I've packaged these java files I get six new errors, all pertaining to the class Player:

Owner-Owners-MacBook:Code OwnerOwner$ javac GuessGame.java
GuessGame.java:3: cannot find symbol
symbol : class Player
location: class guessnum.GuessGame
Player p1;
^
GuessGame.java:4: cannot find symbol
symbol : class Player
location: class guessnum.GuessGame
Player p2;
^
GuessGame.java:5: cannot find symbol
symbol : class Player
location: class guessnum.GuessGame
Player p3;
^
GuessGame.java:8: cannot find symbol
symbol : class Player
location: class guessnum.GuessGame
p1 = new Player();
^
GuessGame.java:9: cannot find symbol
symbol : class Player
location: class guessnum.GuessGame
p2 = new Player();
^
GuessGame.java:10: cannot find symbol
symbol : class Player
location: class guessnum.GuessGame
p3 = new Player();
^
6 errors




I think I may be taking more steps than is necessary to compile and run this, since the textbook hasn't mentioned packaging or classpaths yet. I'm about to throw in the towel on this program and just keep reading, it just seemed like a good idea to actually get some code working before I got too deep into the book.
 
Henry Wong
author
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony Schmitt wrote:
I think I may be taking more steps than is necessary to compile and run this, since the textbook hasn't mentioned packaging or classpaths yet.


Well, at this point, you should not need to set any classpaths -- as your program is relatively simple. As for using packages, it may be a good idea to not use it, until you understand it better.

Anthony Schmitt wrote:
I'm about to throw in the towel on this program and just keep reading, it just seemed like a good idea to actually get some code working before I got too deep into the book.


You will have to get through this regardless. Either you do it now, or do it later, the next time you want to get some code working.

Henry
 
Anthony Schmitt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're absolutely right. I was thinking they might supply me with more tools later on, but they specifically mention they will come back to this program later. So, we're back to square one.

the class GameLauncher wasn't created when I compiled GuessGame.java. Isn't it supposed to do that? But.... I just noticed the GameLauncher class isn't defined anywhere in GuessGame.java, so I guess it isn't supposed to make a class out of the java file.

According to the text I'm supposed to be able to compile GuessGame.java then run GameLauncher.

I have GuessGame.class and Player.class, those compiled just fine.

What do I need to do now to run GameLauncher?

Player.class compiled automatically when i compiled GuessGame.java, by the way. That's what led me to believe GameLauncher.java would behave similarly.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a WAG on my part, but since GuessGame NEEDS a player, when you compile former, it compiled the latter.

Try compiling GameLauncer with

javac GameLauncher.java

then run it with

java GameLauncher
 
Anthony Schmitt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was it!

By the way, what is a WAG? Google indicated that you may either be referring to Walgreen's or the wives and girlfriends of European football players.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoever sent a PM suggesting you use package names was probably mistaken on two counts: we don't like answers by PM or e-mail, and you probably don't need package names for an applicaittion with only 3 classes in.
 
Anthony Schmitt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still curious about wag though.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry... it's a Wild-A**-Guess.
 
Anthony Schmitt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha! Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic