+ 2
How do I launch this java numbers guessing game?
I'm working on a numbers guessing game out of the head first java book and I cant get it to work. I've got 3 public classes: GameLauncher, Player and GuessGame. Only the GameLauncher class has main method in it. Are these all suppose to be in one file or should they be one inside the other or what? I know the public class usually has to be the file name as well so I assume they're supposed to be in separate files. But anytime I put them in separate files cmd says that I dont have a main method in GuessGame(which is the main syntax of the game). Not sure what to do. Surely the Head First Java book isnt wrong but you never know lol. Any help is appreciated. Thanks!
4 Answers
+ 8
Usually you have one class per file (except inner classes, but I guess you'll learn about these later on).
The reasons why the jvm cannot find the main methods are:
1. You're running the wrong class (GuessGame), but should run GameLauncher, which contains the main method.
2. The main method in GameLauncher isn't declared correctly.
I think it is reason 1 in your case. You might need to fix the class path.
I'll provide a link because the first answer there is somewhat perfect ;)
https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean
+ 2
Lol thanks Tashi N!
+ 1
You can have only one public class in a given file and public class usually contains main method
So in your case because all three classes declared public you should put them in seprate files and take care to compile all three with javac [filename.java] and then run only luncher class with command Java [luncher class]
Or you can declare other two classes without public and put all three in a single file and compile and then run it in cmd
0
u can