Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Applet Issues

  1. May 25, 2012 #1

    I am currently enrolled in an introductory Computer Science course, and I've been having trouble with a Java/HTML project. I have been given the following code for a triangle applet to be linked in an HTML page I have to make:

    import java.awt.*;
    import java.applet.Applet;
    public class Triangle extends Applet {
    public void paint (Graphics g){
    int bottomX=80;
    int bottomY=200;
    int base=100;
    int height=100;
    g.drawLine(bottomX+base/2,bottomY-height, bottomX,bottomY);

    I've searched for solutions on the Web but have not figured out how to resolve the following error on the DOS command prompt:

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

    I have been able to compile the Triangle.java file using the command: javac Triangle.java but when I enter: java Triangle, I get the above error. When I tested it using the appletviewer, the class was not found. I am currently using Windows 7 and have the Java SE Development Kit 6 Update 32 installed. I would really appreciate any input as I don't know what I'm doing wrong.
  2. jcsd
  3. May 26, 2012 #2


    Staff: Mentor

    Have you written a main method? That's what the error is telling you.

    When you post code, do us a favor and put [noparse]
    Code (Text):
    [/noparse] tags around it. This makes it easier to understand you code, if you are indenting it - hint, it's a good idea to do this, something like I did below.

    Here's a link to a bunch of java applets, with code, on the sun site: http://java.sun.com/applets/jdk/1.4/index.html.

  4. May 26, 2012 #3
    Thanks for your reply! I will remember to put the code around tags the next time. I read about having to insert a main method. However, the following website:


    and others have stated that applets don't have the main method while in an application, execution starts with the main method. I am working with an applet rather than an application. I've been asked to enter the code as it has been specified in the project into my editor. Does it make a difference that I copied and pasted the code into the editor? I used 'Programmer's File Editor'. I'm stuck at this step!
  5. May 26, 2012 #4


    User Avatar
    Science Advisor

    I haven't done any applet programming but you will need to know where the entrypoint is for your applet (i.e. your main or similar for your applet) and also what kind of applet/program it is.

    What your code looks like is some kind UI widget that implements drawing functionality for a particular class. If this is the case, then you need to understand how that class is used in this context, how the applet recognizes the class and ultimately what kind of interaction is going on between the applet and your class.

    The best way to find this out is to look at the applet and then see what kind of class your triangle is, what it's interface is, how to implement said functions and then how this is used ultimately in the applet and anything else (like your actual running program, external data file, etc).

    First thing I would do is find out what the interface for your triangle object has to have and then take it from there. This will answer more questions of yours than just the one you have.

    If you don't know where to start, you might want to tell us what applet you are dealing with, what it actually does, and what you are trying to implement with this triangle class.
  6. May 26, 2012 #5
  7. May 26, 2012 #6


    User Avatar
    Science Advisor

    Did you put it in the right directory? Are all class files and associated compiled and uncompiled applet files in the same working directory?

    Usually if you are linking different modules together they are likely going to be in the same directory and the linker (or the VM) will also assume this one way or another.
  8. May 26, 2012 #7
    Yes, the triangle.java, triangle.class and triangle.htm are all in the same directory. It won't show up through the appletviewer or when I try to click through on an html page. The main error appears and the class is not found as well.
  9. May 26, 2012 #8


    User Avatar
    Science Advisor

    I had a look at the HTML code and it says it is running only the class file. I checked this with the following and it looks ok.


    One thing I would try and do is to check if you have the latest Java SDK and run appletviewer yourfile.html where the yourfile.html is the html file. I'd also check that you have the latest Java Runtime for your browser as well, but I think this is not going to be an issue.

    Try running appletviewer on your html and see what happens.
  10. May 26, 2012 #9


    User Avatar
    Science Advisor
    Homework Helper

    That's what the message "says", but what it's telling you is that you are trying to run your applet as a stand-alone java prrogram, not as an applet.

    Applets don't need a "main" method. Use the "init" method to do any startup operations, and "paint" to redraw the screen.

    To run an applet, you can either create a web page with an <APPLET> tag and view the page on your browser, or use Sun's "applet viewer" to run it outside of a web page.
  11. May 26, 2012 #10


    Staff: Mentor

    Thanks, I didn't remember that - it's been 17 years since I wrote any Java applets, so I'm a bit rusty.
  12. May 26, 2012 #11
    I have tried running the appletviewer on my html file, but I still get a message that the class was not found, even though Triangle.java, Triangle.class and Triangle.htm are all in the same directory. Here is my code for Triangle.htm:

    Code (Text):



    <TITLE> Project Three: HTML and Java </TITLE>


    <applet code=“Triangle.class“ width=400 height=400>



    I also tried placing the java file into the bin folder of the JDK directory, compiling it and running it; however, I got the same message.
  13. May 26, 2012 #12


    User Avatar
    Science Advisor

    Just a few ideas: try removing the quotes from "Triangle.class" to make it Triangle.class. Also make sure you retain case sensitivity between your file name and your class name (which you have done: make sure your file is Triangle.class with a capital T), and also make sure you have the latest JDK and that any path variables are set to the right values.
  14. May 26, 2012 #13
    Thank you so much, chiro! Removing the quotes from "Triangle.class" solved the problem. I really appreciate everyone who helped me through the problem.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook