954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Programme Compiled but not Run

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

file name - MyCanvas.java
import 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 url(s)

where include:
-debug Start the applet viewer in the Java debugger
-encoding Specify character encoding used by HTML files
-J 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.

pardeep3dec
Junior Poster in Training
90 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

its a right code..
just add applet tag n then try to run
/*

gur
Newbie Poster
2 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

just add applet tag and run this program as mentions in above reply....

enjoy

rohitrohitrohit
Junior Poster
128 posts since Oct 2007
Reputation Points: 15
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You