Hi, I just finished up AP Computer Science a few weeks ago and decided to write an app that allows me to quiz myself with vocab words and definitions, it works fine while I'm running it in IDE, but when I make it a JAR it refuses to open (I'm on OSX), I tried making it an application, but have had no luck. I would like the app to open via terminal so that it can be run in command line for now, in the future I will learn how to make a GUI for the program..could anyone point me in the right direction for learning GUI in java (I use NetBeans) along with helping me get my app running so I can distribute it among my eager friends?

It's not close to finished...but here's the work in progress..

package QuizMe;
import java.util.Scanner;
public class QuizMe {
    public static void main(String[] args) {
        boolean run = true;
            System.out.print("Please enter the number of words you would like to learn: ");
            Scanner input = new Scanner (System.in);
            int amount = input.nextInt();
            int count = 0;
            String[] terms = new String[amount];
            String[] answers = new String[amount];

            for (int i = 0; i < terms.length; i++) {
                System.out.printf("Please enter term #%d: ", i + 1);
                terms[i] = input.next();
                System.out.printf("Please enter answer #%d: ", i + 1);
                answers[i] = input.next();
            }
            while (run) {
                System.out.println("");
                System.out.println("Would you like to...");
                System.out.println("[1]. Edit your terms/answers");
                System.out.println("[2]. Add more terms/answers");
                System.out.println("[3]. Remove terms/answers");
                System.out.println("[4]. Be Quizzed");
                System.out.println("[5]. Exit the program\n");
                System.out.print("Please enter a number choice: ");
                int menuinput = input.nextInt();

                
//                if (menuinput == 3) {
//                    System.out.println("Would you first like to view the terms and answers? [Y]es/[N]o? ");
//                    String yesno = input.next();
//                    if (yesno.equals("Y")) {
//                        System.out.println("Terms:");
//                        for (int i = 0; i < terms.length; i++) {
//                            System.out.printf("Term #%d: " + terms[i] + "\t\n", count + 1);
//                        }
//                        System.out.println("\nAnswers:");
//                        for (int i = 0; i < answers.length; i++) {
//                            System.out.printf("Answer #%d: " + answers[i] + "\t\n", count + 1);
//                        }
//                    }
//
//                    System.out.println("Please enter which term/answer you would like to be deleted: ");
//                    int delete = input.nextInt();
//                    if (delete > terms.length || delete < terms.length) {
//                        System.out.println("Please enter a valid term/answer number...");
//                    } else {
//                        terms[delete - 1] = null;
//                        answers[delete - 1] = null;
//                        
//                    }
//                }

                if (menuinput == 4) {
                    while (count < terms.length) {
                    System.out.printf("Term #%d: " + terms[count] + "\n", count + 1);
                    System.out.println("Press enter to see the answer for this term...");
                    input.nextLine();
                    System.out.printf("Answer #%d: " + answers[count] + "\n", count + 1);
                    System.out.println("");
                    count++;
                }
                    System.out.println("You have finished being quized!");
                }

                if (menuinput == 5) {
                    System.out.print("Are you sure you want to quit, you will lose any entered information? [Y]es/[N]o? ");
                    String yesno = input.next();
                    if (yesno.equals("Y")) {
                        System.out.println("Goodbye!");
                        run = false;
                    } else {
                        continue;
                    }
                }
            }
    }
}

Recommended Answers

All 20 Replies

it refuses to open

Do you get any error messages? Please copy and paste full text here.
Does your jar file have a correctly formatted Manifest file?

When I followed this http://www.centerkey.com/mac/java/, after step 5 it opened perfectly in terminal, but after I finished the tutorial (making it a .app) it refused to open. Here's the message I get when I try to open it without doing all those steps. The Java JAR file "Quiz_Me.jar" could not be launched. Check the Console for possible error messages.

When I check the Console the following appears: 6/14/10 6:06:48 PM [0x0-0x180180].com.apple.JarLauncher[26542] Exception in thread "main" java.lang.NoClassDefFoundError: quizme/Main
6/14/10 6:06:48 PM [0x0-0x180180].com.apple.JarLauncher[26542] Caused by: java.lang.ClassNotFoundException: quizme.Main
6/14/10 6:06:48 PM [0x0-0x180180].com.apple.JarLauncher[26542] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
6/14/10 6:06:48 PM [0x0-0x180180].com.apple.JarLauncher[26542] at java.security.AccessController.doPrivileged(Native Method)
6/14/10 6:06:48 PM [0x0-0x180180].com.apple.JarLauncher[26542] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
6/14/10 6:06:48 PM [0x0-0x180180].com.apple.JarLauncher[26542] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
6/14/10 6:06:48 PM [0x0-0x180180].com.apple.JarLauncher[26542] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
6/14/10 6:06:48 PM [0x0-0x180180].com.apple.JarLauncher[26542] at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

*I tried sending the JAR file to a few friends of mine who have Macs and they encountered the same problem as I.

NoClassDefFoundError: quizme/Main

Java can't find the class file: quizme.Main.class
Is the Main.class file in the jar file with the correct path. IE in the folder quizme.
What are the contents of the Manifest.MF file?
I suspect it has an entry: Main-Class: quizme.Main

The Main.class file must be in the quizme folder in the jar file.

The only class in the package is the one posted above, I made the JAR with NetBeans, and this is what the Manifest.MF file contains:

Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

only class in the package

Is the class file in a folder in the jar file?

How are you trying to execute the program? From what you've shown, I don't see how the java program knew to look for the quizme/Main class.

Ideally I would like to run the program in command prompt/terminal, but I don't know how to do that. Given the single QuizMe.java class, how would I go about doing that?

Given the single QuizMe.java class

That's a strange name for a class file: QuizMe.java.class. Is that what you meant?
What is the name of the class file and what is the name of the package that the class is in?

Say the class filename is: Main.class
and it is in the package: QuizMe

Then cd to the folder containing the QuizMe folder containing the Main.class file and enter the command:
java QuizMe.Main

I use NetBeans, and in NetBeans I made a main class file (the one shown above) called QuizMe (the .java extension is added automatically) in a package called QuizMe in a project called QuizMe. Maybe I should try re-doing it in Eclipse..

When I followed your instructions and got the same error that showed up in console earlier.

I use NetBeans,

Don't use an IDE to execute it. Go to a command prompt as I posted above and enter the java command there. Java is case sensitive.

Can you copy the full contents of the command prompt window and paste it here. Show the contents of the QuizMe folder also.

Don't use an IDE to execute it. Go to a command prompt as I posted above and enter the java command there. Java is case sensitive.

Can you copy the full contents of the command prompt window and paste it here. Show the contents of the QuizMe folder also.

I did it all in command prompt, using the directory made by the IDE. The error that it gave was identical to the one in the console earlier..

I did it all in command prompt

What is the all that you did?

Go to a command prompt as I posted above and enter the java command there. Java is case sensitive.

Can you copy the full contents of the command prompt window and paste it here. Show the contents of the QuizMe folder also.

Since I can't see what's on the screen from here, can you post it? Otherwise you'll have to wait for someone with a crystal ball or better imagination than I have.

Last login: Mon Jun 14 20:01:36 on ttys000
new-host-2:~ Gabe$ cd /Users/Gabe/NetBeansProjects/QuizMe/build/classes/quizme
new-host-2:quizme Gabe$ java QuizMe.Main
Exception in thread "main" java.lang.NoClassDefFoundError: QuizMe/Main
Caused by: java.lang.ClassNotFoundException: QuizMe.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
new-host-2:quizme Gabe$

Show the contents of the QuizMe folder also.

I don't see the contents of the QuizMe folder.

I notice that the folder name is quizme not QuizMe. Java is case sensitive.

new-host-2:quizme Gabe$ java QuizMe.Main

Does this show that you are IN THE quizeme folder?
You're supposed to be in the folder that contains the quizme folder. IE Up one level.

Yep, you're right, my bad. It now works. But...how do I have it so that people who I send it to who aren't familiar with using terminal/comand prompt can just click something once and have it open in terminal?

new-host-2:~ Gabe$ cd /Users/Gabe/NetBeansProjects/QuizMe/build/classes
new-host-2:classes Gabe$ quizme.Main
-bash: quizme.Main: command not found
new-host-2:classes Gabe$ java quizme.Main
Please enter the number of words you would like to learn:

On WinXp and some other OSs, you put the class files in a jar file with the classfiles in the folders matching their packages. The jar file also has a Manifest file as I posted earlier.
On WinXP when you double click on a jar file, the following command is executed:
java -jar <jarfilename>

java reads the jar file, extracts the Main-Class info from the manifest and starts that class at its main() method.

No idea how to use your IDE to create the jar file. I have dozens of batch files I use to create my jar files.
Here's a sample:

REM Make RM_Client.jar file
%DEV_DRIVE%
SET JarName=%DEV_HOME%\JavaDevelopment\NormsDev\RemoteMouse\RM_Client.jar

cd %DEV_HOME%\JavaDevelopment\NormsDev\RemoteMouse
jar -cmf RM_Client.mnf %JarName% RM_Client*.class

@REM Now pick up the Tools it needs:

cd %DEV_HOME%\JavaDevelopment\
jar -uf %JarName% NormsTools\KeepAliveBroadcast\DataToSend*.class NormsTools\KeepAliveBroadcast\KeepAliveBroadcastingServer*.class NormsTools/KeepAliveBroadcast/BroadcastTarget*.class NormsTools/KeepAliveBroadcast/HandleConnect*.class NormsTools/KeepAliveBroadcast/MessageListener*.class NormsTools\SaveStdOutput*.class
@ECHO ---- Created: %JarName% ----
MORE

I'm not sure how I would go about doing that on a mac, maybe someone who is more familiar with how to do this on a mac would be of more use to me. Thanks for your help though.

Sorry for the triple post.
Using http://www.centerkey.com/mac/java/ I've figured out that I need to name the class Main.java for that tutorial to work, using the tutorial on the site I've made an OSX app, but unfortunately because the app doesn't have a GUI, it is run in console and crashes because the user doesn't have a chance to input. Is there any way to have a terminal window pop up so that a user can use the program?

More concisely, is there a way that when the QuizMe.app is clicked on, the command

java -jar QuizMe.jar

is executed and the program is opened in terminal?

Working code:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        boolean run = true;
        System.out.print("Please enter the number of words you would like to learn: ");
        Scanner input = new Scanner (System.in);
        int amount = input.nextInt();
        int count = 0;
        String[] terms = new String[amount];
        String[] answers = new String[amount];

        for (int i = 0; i < terms.length; i++) {
            System.out.printf("Please enter term #%d: ", i + 1);
            terms[i] = input.next();
            System.out.printf("Please enter answer #%d: ", i + 1);
            answers[i] = input.next();
        }

        while (run) {
            System.out.println("");
            System.out.println("Would you like to...");
            System.out.println("[1]. Edit your terms/answers");
            System.out.println("[2]. Be Quizzed");
            System.out.println("[3]. Start a new quiz");
            System.out.println("[4]. Exit the program\n");
            System.out.print("Please enter a number choice: ");
            int menuinput = input.nextInt();

            if (menuinput == 1) {
                System.out.print("Would you first like to view the terms and answers? [Y]es/[N]o? ");
                String yesno = input.next();
                if (yesno.equals("Y") || yesno.equals("y")) {
                    System.out.println("Terms:");
                    for (int i = 0; i < terms.length; i++) {
                        System.out.printf("Term #%d: " + terms[i] + "\t\n", i + 1);
                    }
                    System.out.println("\nAnswers:");
                    for (int i = 0; i < answers.length; i++) {
                        System.out.printf("Answer #%d: " + answers[i] + "\t\n", i + 1);
                    }
                }
                System.out.print("Which term/answer would you like to edit? ");
                int edit = input.nextInt();
                System.out.printf("What would you like to change term#%d to? ", edit);
                String editterm = input.next();
                terms[edit + 1] = editterm;
                System.out.printf("\nWhat would you like to change answer#%d to? ", edit);
                String editanswer = input.next();
                answers[edit + 1] = editanswer;
                System.out.printf("\nEntry #%d was changed successfully!", edit);
            }

            if (menuinput == 2) {
                while (count < terms.length) {
                    System.out.printf("Term #%d: " + terms[count] + "\n", count + 1);
                    System.out.println("Press enter to see the answer for this term...");
                    input.nextLine();
                    input.nextLine();
                    System.out.printf("\nAnswer #%d: " + answers[count] + "\n", count + 1);
                    count++;
                }
                System.out.println("You have finished being quized!");
            }

            if (menuinput == 3) {
                System.out.println("Enjoy your next quiz!");
                break;
            }

            if (menuinput == 4) {
                System.out.print("Are you sure you want to quit, you will lose any entered information? [Y]es/[N]o? ");
                String yesno = input.next();
                if (yesno.equals("Y") || yesno.equals("y")) {
                    System.out.println("Goodbye!");
                    run = false;
                } else {
                    continue;
                }
            }
        }
    }
}

Sorry, that's an OS related question. On Windows I'd use a batchfile to start a command prompt and execute the java program. No ideas about your OS.

I've decided to learn Java GUI to make the app better, thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.