Java code doesn't do anything?

inadaze@mac.com

Registered
Hi,
I am trying a tutorial and nothing happens. I am using xCode java tool and in a folder I have GameLauncher.java,Player.java and GuessGame.java.
Here is the code in each:

//
// GuessGame.java
// GuessGame
//
// Created by Jay on Tue Oct 26 2004.
// Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

public class GuessGame {
//GuessGame has three variables for each player
Player p1;
Player p2;
Player p3;


public void startGame() {
//create three objects for the three variables
p1 = new Player();
p2 = new Player();
p3 = new Player();
//declare three variables to hold player guess
int guessp1 = 0;
int guessp2 = 0;
int guessp3 = 0;
//declare three variables to hold a true or false player answer
boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false;

//Make a number for player to guess
int targetNumber = (int) (Math.random() * 10);
System.out.println("I'm thinking of a number between 0 and 9...");

while(true) {
System.out.println("Number to guess is "+targetNumber);

//call each players guess() method
p1.guess();
p2.guess();
p3.guess();

//get each players guess
guessp1 = p1.number;
System.out.println("Player one guessed " + guessp1);
guessp2 = p2.number;
System.out.println("Player one guessed " + guessp2);
guessp3 = p3.number;
System.out.println("Player one guessed " + guessp3);

//test each players answer
if (guessp1 == targetNumber){
p1isRight = true;
}
if (guessp2 == targetNumber){
p2isRight = true;
}
if (guessp3 == targetNumber){
p3isRight = true;
}

//give results of the game
if (p1isRight || p2isRight || p3isRight){
System.out.println("We have a winner!");
System.out.println("Player 1" + p1isRight);
System.out.println("Player 2" + p2isRight);
System.out.println("Player 3" + p3isRight);
System.out.println("The Game is Over!");
break;
} else {
System.out.println("No players were right!!");
}
}
}
}

//
// Player.java
// GuessGame
//
// Created by Jay on Tue Oct 26 2004.
// Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

public class Player {
//Variable to hold the guess
int number = 0;

public void guess(){
number = (int) (Math.random() * 10);
System.out.println("I'm guessing " + number);
}
}

//
// GameLauncher.java
// GuessGame
//
// Created by Jay on Tue Oct 26 2004.
// Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

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

When I try the generate .jar file it gives me an error "The jar file "GuessGame.jar" couldn't be launched. Check the Console for possible error messages."
The run log says "Exception in thread "main" java.lang.NoSuchMethodError: main

java has exited with status 1."

And that is all. Any suggestions?

Thanks
Jay
 
Ah, that's an easy fix. That error message means it can't find the main() method in the specified startup class.

See that Manifest file in the project? Edit that. Change the Main-class definition to the name of the class that actually has the main() method in it (in this case, GameLauncher).

When you create a new Java project, Xcode assumes the java source file that it automatically creates with the same name as the project will have your main() in it. If you put it somewhere else, you'll have to tell it where to look.
 
Cool! thanks!
So in Xcode, the project name always has to be the same as the file with the main class in it, along with the code in the Manifest?

Thanks
Jay
 
Nope. You can put your main() method wherever the heck you please. Just make sure that if you put it somewhere other than the default (ProjectName.java), edit the Manifest file so the java runtime knows where to look for it.
 
Back
Top