7 Replies Latest reply: Jun 3, 2007 8:10 PM by 807606 RSS

    could not find the main class, program will exit....

    807606
      Hi all,

      I am trying to make a panel for my later animation. But when I ran the lines posted below to test it, i got the error message just as what I typed in the subject. And the output is
      "java.lang.NoClassDefFoundError:BouncingBall/BouncingBall" .

      Exception in thread "main"

      Can someone please explain to me what mistakes I made and how to avoid them? Thanks.


      Code:
      package BouncingBall;

      import java.awt.Graphics;
      import javax.swing.JFrame;
      import javax.swing.JPanel;


      public class BouncingBall extends JFrame {
           Graphics g;
           
           public static void main(String args[]) {
                
      BouncingBall window = new BouncingBall();
      window.run();
      }

      void run() {

      JPanel panel = new JPanel();
      add(panel);
      setSize(500, 400);
      setVisible(true);
      g=panel.getGraphics();
      g.fillOval(70, 70, 100, 100);
      }

      Message was edited by:
      kevinchang
        • 1. Re: could not find the main class, program will exit....
          807606
          Did you compile the class using 'javac'? Since the specified code is missing an end brace, either it hasn't been compiled or you pasted incorrectly. Also, is your classpath specified properly?
          • 2. Re: could not find the main class, program will exit....
            807606
            Also if you are making a jar of this code and running thru it then check the Manifest file. it should mention the main class.
            • 3. Re: could not find the main class, program will exit....
              807606
              "Could not find the main class" is what you get when you have not specified the main class properly.

              "Program wil exit..." looks like a message from an IDE (a program source editor). This is really bad news as it suggests you may not conscious of how you are specifying the main class.

              Ignoring the IDE for the moment, the code you posted should be in a file called:
              BouncingBall.java
              And that file must be in a directory called the same as its package:
              BouncingBall
              This package can be anywhere on your disk. In what follows I'll assume it is in c:\java\ First correct your program - because it won't compile at the moment - by adding a } missing at the end. Then:
              C:\>cd \java
              C:\java>javac -cp . BouncingBall\BouncingBall.java
              C:\java>java -cp . BouncingBall.BouncingBall
              What these steps do is
                * Navigate to the working directory
                * Compile the source file (specified by its filename) using the current directory as the classpath (that's what -cp . is all about)
                * Runs the main class (specified by its full class name)

              If you get stuck, elaborate those commands with a few more which give diagnostic information:
              C:\>cd \java
              C:\java\>dir
              C:\java\>path
              C:\java\>javac -version
              C:\java\>javac -cp . BouncingBall\BouncingBall.java
              C:\java\>dir
              C:\java\>java -cp . BouncingBall.BouncingBall
              Copy and post the actual output. Remember that these examples assume hat you are working from a directory called "java". If you are using Linux and can't translate the commands, say.

              A few points:

              (1) It would be much better to call the package bouncingBall, not BouncingBall. Using small letters at the start of package names is a convention. Both the .java source file and the directory name need to be changed.

              (2) You have written no code to make your program exit. From the Windows command line you can make it exit by typing Control+C

              (3) The stuff you do with the Graphics object looks a little odd. You might want to have a look at Sun's Tutorial section on Graphical User Interfaces (http://java.sun.com/docs/books/tutorial/ui/index.html) or other bits of the Tutorial (http://java.sun.com/docs/books/tutorial/index.html)
              • 4. Re: could not find the main class, program will exit....
                807606
                Sorry. I post it incorrectly. However, the one with correct braces didn't work out . I used Eclipse to write , so I didn't really have command-lines involving javac. The only thing about the classpath I find so far is a file named .classpath with the content posted below.

                "<?xml version="1.0" encoding="UTF-8"?>
                <classpath>
                     <classpathentry path="" kind="src"/>
                     <classpathentry path="org.eclipse.jdt.launching.JRE_CONTAINER" kind="con"/>
                     <classpathentry path="" kind="output"/>
                </classpath>"

                I still don't know how to fix the problem.But thanks for you help.
                • 5. Re: could not find the main class, program will exit....
                  807606
                  I think he just needed a simple run, not running from jar file.
                  Simple example of compilation and running of java classes with java package used. With the use of java package, it is a bit different. Below is just an example. Run it from the "Root" of the directory.

                  To compile from command line:
                  javac BouncingBall\BouncingBall.java

                  If above java compilation completed successfully, run from command line:
                  java BouncingBall.BouncingBall


                  Hope it helps.
                  • 6. Re: could not find the main class, program will exit....
                    807606
                    Eclipse should smart enough to figure out the classpath.

                    Right click on the BouncingBall.java file and choose
                    Run As -> Run Java application
                    • 7. Re: could not find the main class, program will exit....
                      807606
                      Eclipse should smart enough to figure out the
                      classpath.

                      Right click on the BouncingBall.java file and choose
                      Run As -> Run Java application
                      I think that you need to configure things in eclipse.