LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   java applet:: main method not found (http://www.linuxquestions.org/questions/programming-9/java-applet-main-method-not-found-4175433714/)

amitshree 10-23-2012 01:10 PM

java applet:: main method not found
 
java prgrams is simply running in fedora17 terminal but when trying to run Applets then it shows the error
Error: Main method not found in class HelloWorld, please define the main method as:
public static void main(String[] args)

kedarp 10-23-2012 02:22 PM

Applets don't have a main method in Java. Then how can your program get the output.
Quote:

Error: Main method not found in class HelloWorld, please define the main method as:
public static void main(String[] args)
Did you extend the Applet class?
Quote:

public class HelloWorld extends Applet

amitshree 10-24-2012 10:25 AM

i have extended the HelloWorld class but as the programs given in the book java the complete reference any of the program doesn't contain main method....and the same program i am able to run from eclipse but not from terminal

amitshree 10-24-2012 10:57 AM

here is a program sample
import java.awt.Graphics;
public class HelloWorld extends java.applet.Applet {
public void init() {
resize(150,25);
}
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}

kedarp 10-25-2012 12:02 PM

Code:

import java.awt.Graphics;


/*<applet code="HelloWorld"></applet>*/


public class HelloWorld extends java.applet.Applet {

    public void init()
    {
    resize(150,25);
    }
    public void paint(Graphics g)
    {
    g.drawString("Hello world!", 50, 25);
    }
}

The code is correct. But when you run from terminal, it will not run till you add the bold lines.

Applets are loaded in a browser. So, either you can create a HTML file named HelloWorld and add the <applet> tag to it.
Or you can add the tag in the Java file itself as above.
And run the program.
Code:

$javac Helloworld.java
$appletviewer Helloworld.java



All times are GMT -5. The time now is 12:19 AM.