1

I Cant understand why this messege come--------->

java.lang.NoSuchMethodError: main
Exception in thread "main" .

I know it expecting main() method but as i m building an applet which does not contain main method rather contain init() method.So what will i do??My code is s follow --->

import java.applet.*;
import java.awt.*;

public class Ballbewegung1 extends Applet implements Runnable
{
// Initialisierung der Variablen

    int x_pos = 10;     // x - Position des Balles
    int y_pos = 100;    // y - Position des Balles
    int radius = 20;    // Radius des Balles

    public void init()
    {
        setBackground (Color.blue);
    }

    public void start ()
    {
        // Schaffen eines neuen Threads, in dem das Spiel lไuft
        Thread th = new Thread (this);
        // Starten des Threads
        th.start ();
    }

    public void stop()
    {

    }

    public void destroy()
    {

    }

    public void run ()
    {
        // Erniedrigen der ThreadPriority um zeichnen zu erleichtern
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

        // Solange true ist lไuft der Thread weiter
        while (true)
        {
            // Verไndern der x- Koordinate
            x_pos ++;

            // Neuzeichnen des Applets
            repaint();

            try
            {
                // Stoppen des Threads fr in Klammern angegebene Millisekunden
                Thread.sleep (20);
            }
            catch (InterruptedException ex)
            {
                // do nothing
            }

            // Zurcksetzen der ThreadPriority auf Maximalwert
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }
    }

    public void paint (Graphics g)
    {
        g.setColor  (Color.red);
        g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
    }
}

And I dont know how to use code tag.so plz someone ans.

Peter Lang
  • 50,721
  • 26
  • 139
  • 153
russell
  • 3,468
  • 8
  • 39
  • 56
  • I got the solution .the Steps as follows >1.compile java file.though it does not run,it creates a .class file.then it need to embedded into a html file.In html file : and then just run the html file in browser. – russell May 20 '10 at 11:43
  • This Community Wiki question lists the possible causes of this common problem: http://stackoverflow.com/questions/5407250/causes-of-java-lang-nosuchmethoderror-main-exception-in-thread-main – Stephen C Jun 28 '11 at 14:39

2 Answers2

5

The applet needs a container to run. Embed it in a html page and Use appletviewer or a web browser to run it

Midhat
  • 16,422
  • 17
  • 84
  • 113
1

Or if you're using Eclipse you need to run it as an applet: right click on class -> Run as -> Java Applet (I'm not sure about the other IDEs but I guess they have similar running options).

Andrei Fierbinteanu
  • 7,218
  • 3
  • 29
  • 45