-1

I created a small game from my Java book of exercises, when I compile it in Eclipse it runs fine, but once I add the main method and export it as a .jar onto my desktop it does not work when I double click it, neither does it work on the command console. I cant seem to export any funtioning .jar program, although they compile well in Eclipse. I assume I am doing something wrong when using the main method?

    import acm.program.*;
    import acm.util.*;

    import java.awt.Color;

    import acm.graphics.*;

    public class CrapsGameTest extends ConsoleProgram{


public static void main (String[] args){
    CrapsGameTest craps = new CrapsGameTest();
    craps.run();


}  


    public void run() {

/*if total = 2, 3, or 12 you loose
 * if total = 7 or 11 you win
 * 
 * */
setFont("Helvetica-40");


int total = rollTwoDice();

if (total == 2 || total == 3 || total==12) {
    println(total +" You loose");
} else if (total == 7 || total == 11) {
        println(total +" You win");
    } else {
        //saves points under the initial TOTAL variable
        int points=total;
        println("your point total is " + points);

        //initializes step by step rolls with i++
        int i = 0;
        while(i>=0) {
            int x = readInt("Roll again?");


            if (x==1) {
                i++;
            }
            //changes the total variable by rolling dice.
            //if the new total variable = points then you win:
            //after every loop "total" variable changes due to the rollTwoDice method.
            total = rollTwoDice();

            if (points ==total) {
                println("You win!");
                break;
                } else if (total == 7) {
                    println("you loose");
                    break;


            }



        }
        }
int y = readInt("Do you wish to play again?");
if (y == 1) {
    run();

}
    }


    private int rollTwoDice() {
int d1 = rgen.nextInt(1,6);
int d2 = rgen.nextInt(1,6);
int total = d1 + d2;
println("Rolling dice: " + d1 + " + "+d2 + " = " + total );
return total;


 }



 //initializes rgen variable
 private RandomGenerator rgen = new RandomGenerator();



 }
Pav
  • 57
  • 9
  • 1
    *but once I add the main method* How could it run without a `main` method? – Scary Wombat Jun 14 '18 at 02:44
  • 1
    Have you added manifest file ? – Gaurav Srivastav Jun 14 '18 at 02:48
  • Show us please your jar files – Rcordoval Jun 14 '18 at 03:05
  • Look at this [JAR Files](https://web.stanford.edu/class/archive/cs/cs106a/cs106a.1144/handouts/330%20JAR%20Files.pdf) – Rcordoval Jun 14 '18 at 03:51
  • 1
    Possible duplicate of [Java Eclipse: Difference between exporting as a JAR and exporting as a Runnable JAR](https://stackoverflow.com/questions/4974693/java-eclipse-difference-between-exporting-as-a-jar-and-exporting-as-a-runnable) – AxelH Jun 14 '18 at 05:40
  • 1
    Solution found, thank you @Rcordoval for the pdf. I changed the main method to the following: public static void main (String[] args){ new CrapsGameTest().start(args); } Now it works perfectly fine. – Pav Jun 14 '18 at 11:16

1 Answers1

0

According to this document:

a Java program needs to start at a particular method in a class, the public static void main(String[] args) method. If your program uses the ACM libraries, you’ll need to edit your code to have a main() . You can do this easily by simply adding the following code to the class that has the public void run() , substituting the name of that class for your class below.

public static void main (String[] args){
    new CrapsGameTest().start(args);
}

After that small change, your export should be working.

Rcordoval
  • 1,932
  • 2
  • 19
  • 25