Dear Friends,
Here i am giving my programme coding and error.

file name - MyCanvas.javaimport java.awt.*;
import java.applet.*;


public class MyCanvas extends Applet {
public MyCanvas() {
setSize(80, 40);
}
public void paint(Graphics g) {
g.drawRect(0, 0, 90, 50);
g.drawString("A Canvas", 15,15);
}
}  

When i use command javac MyCanvas.java it is compile but when i use appletviewer
then it shows following error.

E:\pardeep\java\JAVAPROGRAMMES>javac MyCanvas.java


E:\pardeep\java\JAVAPROGRAMMES>appletviewer
Usage: appletviewer <options> url(s)


where <options> include:
-debug                  Start the applet viewer in the Java debugger
-encoding <encoding>    Specify character encoding used by HTML files
-J<runtime flag>        Pass argument to the java interpreter

The -J option is non-standard and subject to change without notice.

E:\pardeep\java\JAVAPROGRAMMES>

Please help i am new to java but i know c/c++ very well.

Edited 3 Years Ago by happygeek: fixed formatting

That would be because you did not follow life cycle of applet, you never actually initialized your applet

Life Cycle of an Applet: Basically, there are four methods in the Applet class on which any applet is built.

  • init: This method is intended for whatever initialization is needed for your applet. It is called after the param attributes of the applet tag.
  • start: This method is automatically called after init method. It is also called whenever user returns to the page containing the applet after visiting other pages.
  • stop: This method is automatically called whenever the user moves away from the page containing applets. You can use this method to stop an animation.
  • destroy: This method is only called when the browser shuts down normally.

Java Sun Tutorials - Lesson Applets READ AND LEARN

Actually, it would be that he never told appletviewer which applet he wanted to view (although your information is good information, he never even got this far).

You need to create a very small static html page that contains a reference to your applet, then run appletviewer as follows

appletviewer <full/or/relative/path/to/html/file>

Edit: One thing from the next post is true, you do not need to create an html page, you can add the applet tag as a comment at the top of the Applet Class as well then enter that as the argument to appletviewer. That does not change the original problem though, which was that you executed appletviewer without an argument, so how was it to know which applet to run?

its a right code..
just add applet tag n then try to run
/*<applet code="MyCanvas.java height=300 width=100>
</applet>*/

or save it as .html n then run in web browser

This article has been dead for over six months. Start a new discussion instead.