Java/main class
Expert: Neal Ziring - 4/6/2005
Questionim having a problem using net beans (java jdk) making a game of hangman.
The problem is that when i try to run any code it says that no main class is found. do you have any suggestions to help solve the problem?
AnswerDominic,
>
> when i try to run any code it says that no
> main class is found
>
There are two possible reasons for this.
1. You may have forgotten to declare a main method, or
declared it incorrectly. To run a Java program, you
effectively call the main method of some particular
class. The main method *MUST* be declared like this:
public static void main(String [] args) {
// your code goes in here
}
So, if you've made any mistakes in declaring this
method, you'd get the message you've observed. The
most common mistake among beginners, in my experience,
is forgetting the method arguments.
2. The other possibility is that you have a bad CLASSPATH.
I don't see how this could happen within NetBeans, but
perhaps it is possible. Anyway, check your CLASSPATH.
Within NetBeans, it is probably set in some kind of
project properties or options setting dialog. In the
shell (command window) it would be set as an environment
variable. To check it on Unix or Linux or MacOS X, use
this command: "env". To check it on Windows, use this
command: "set". If your CLASSPATH does not include the
current directory, ".", then you could get the message
you've observed.
[On Windows, for example, if you've ever installed
the Quicktime Player, it screws up your CLASSPATH.]
To fix the CLASSPATH on Windows, do this:
set CLASSPATH=.;%CLASSPATH%
To fix the CLASSPATH on Linux or most Unixes, do this:
CLASSPATH=.:$CLASSPATH
export CLASSPATH
If none of this works, try looking at the First Cup of Java
section in Sun's Java tutorial:
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/
Good luck...
...nz