Ahmed Awadalla
Ahmed Awadalla

Reputation: 1

Error: Main method not found in class Example_Applet,

The Full Error Message:

[ Main method not found in class Example_Applet, please define the main method as:public static void main(String[] args) ]

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

public class Example_Applet extends Applet {

    String message;

    public void init() {
        message="My first Java Applet";
    }

    public void paint(Graphics g) {
        g.setColor(Color.blue);
        g.drawString(message, 50, 60 );
    }
}

I was trying to replicate my textbooks example applet code. When I try to execute the code in Notebook++ it just prints the code in the browser. Even though it compiles without the errors in JCreator I get the aforementioned error message.Really be huge help if someone could help me with this

Upvotes: 0

Views: 948

Answers (1)

user882813
user882813

Reputation: 835

Applets runs in different way than ordinary java applications. While latter should have public static void main(Strin[] args) method and run with java command the former can be run locally with appletviewer command (bundled with your JDK). In order to run applet locally you should create HTML file with <applet> tag inside. Something like this:

<applet code="Example_Applet.class" width="300" height="300">
  This text will be shown in browsers without java applets support.
</applet>

Place it in same directory as your class Example_Applet.class file (compiled form your Example_applet.java) and then run it with appletviewer YourHtmlFileName.html command.

Future reading: https://docs.oracle.com/javase/tutorial/deployment/applet/html.html

FYI: Applets is quite outdated technology and will not work in most of current browsers. So I suggest you to find not so old textbook as your one.

Upvotes: 1

Related Questions