Re: beginner's question



"Jacky Luk" <jl@xxxxxxxxxx> wrote in news:45116ca0$1@xxxxxxxxx:

import java.awt.Graphics;

public class printtest extends java.applet.Applet
{
public void init()
{
}

public void paint (Graphics g)
{
g.drawString ("Hello from java!", 60, 30);
}

public static void main (String args)
{

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

What happened?
Thanks
Jack

1)
The main method must be declared public and static. Good!
It must not return any value. Good!
It must accept a String array as a parameter. Oops! You have a String, not
a String array.

The method declaration must look like the following:

public static void main(String args[])

The above has been partially copied out of the javadocs for "java".
Get the javadocs and use them. They are your friend.

2)
Note for the future: When java tells you that it cannot find a method, it
is vital to remeber that the method must match both in name and in
signature. Method definitions don't match method calls unless a method of
the correct name can be found that has a parameter list that also matches
the one used for the call.

3)
If I saw an experienced java programmer writing this program, I might
figure that they are writing a mixed application/applet or otherwise doing
something clever -- maybe providing a way to test an applet without
requiring an applet viewer. However, when someone who describes themself
as a beginner writes this way, I get concerned.

Your class extends Applet, indicating that you intend this program to be an
Applet. However, you have a method "main", indicating that you intend this
program to be an application. Furthermore, you used "java" to run it,
again indicating that your intent was for an application.

If your intent was cleverness or a mixed application/applet, please forgive
me. Otherwise, you need to re-think what you are doing and decide whether
you want an application or an applet. Then rethink your design.

Best wishes!



--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
.



Relevant Pages

  • Re: beginners question
    ... It must accept a String array as a parameter. ... public static void main ... When java tells you that it cannot find a method, ... requiring an applet viewer. ...
    (comp.lang.java)
  • Re: How to complie
    ... You compile a java program using "javac", and one of the ways to run the ... public static void translate ... The program you have here is an applet, ...
    (comp.lang.java.programmer)
  • Re: How to complie
    ... You compile a java program using "javac", and one of the ways to run the ... public static void translate ... The program you have here is an applet, ...
    (comp.lang.java.programmer)
  • [Full-Disclosure] Technical information about the vulnerabilities fixed by MS-02-52
    ... The patch doesn't fix all of the vulnerabilities we reported, ... Java support in the Internet Zone even after applying the patch gives the ... possibility for a malicious Java Applet to gain control over the system. ... Our original report and information regarding the remaining Java ...
    (Full-Disclosure)
  • Technical information about the vulnerabilities fixed by MS-02-52
    ... The patch doesn't fix all of the vulnerabilities we reported, ... Java support in the Internet Zone even after applying the patch gives the ... possibility for a malicious Java Applet to gain control over the system. ... Our original report and information regarding the remaining Java ...
    (NT-Bugtraq)