could not find the main class, program will exit....
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
"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.javaAnd that file must be in a directory called the same as its package:BouncingBallThis 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.BouncingBallWhat 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.BouncingBallCopy 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)
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.
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.