Java/main class

Advertisement


Question
im 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?

Answer
Dominic,

>
> 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

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Neal Ziring

Expertise

Almost anything about the core Java platform, with an emphasis on networking. Not familiar with JDBC or the new Java graphics APIs. [Sun Certified Java Programmer, JDK 1.1]

Experience

Certified Java Programmer for JDK 1.1 and 2.0.

©2012 About.com, a part of The New York Times Company. All rights reserved.