Java Java Applet Issues: Solve NoSuchMethodError

  • Thread starter Thread starter SolarMidnite
  • Start date Start date
  • Tags Tags
    Issues Java
AI Thread Summary
The discussion revolves around troubleshooting a Java applet project that is not functioning as expected. The user is facing a "NoSuchMethodError: main" when attempting to run the Triangle applet, despite successfully compiling the Triangle.java file. The confusion stems from the fact that applets do not require a main method, unlike standard Java applications. Key points include the need to run the applet using the appletviewer or through an HTML page with the appropriate <APPLET> tag. The user confirmed that all relevant files (Triangle.java, Triangle.class, and Triangle.htm) are in the same directory. A significant breakthrough occurred when it was suggested to remove the quotes from "Triangle.class" in the HTML code, which ultimately resolved the issue. The conversation highlights the importance of understanding the differences between applet and application execution in Java, as well as ensuring correct file references and syntax in HTML.
SolarMidnite
Messages
20
Reaction score
0
Hello,

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,bottomY,bottomX+base,bottomY);
g.drawLine(bottomX+base,bottomY,bottomX+base/2,bottomY-height);
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.
 
Technology news on Phys.org
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:
 and
[/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.

SolarMidnite said:
Hello,

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:
Code:
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,bottomY,bottomX+base,bottomY);
    g.drawLine(bottomX+base,bottomY,bottomX+base/2,bottomY-height);
    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.
 
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:

http://www.roseindia.net/java/example/java/applet/applet-versus-application.shtml

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!
 
SolarMidnite said:
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:

http://www.roseindia.net/java/example/java/applet/applet-versus-application.shtml

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!

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.
 
SolarMidnite said:
When the applet is run successfully, it should appear as such shown in the link below:

http://ccis.athabascau.ca/html/courses/comp200n/tmesv2/project/triangle.htm

I have Triangle.class after compiling Triangle.java, but for some reason the class cannot be found and I get the main error.

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.
 
chiro said:
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.

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.
 
SolarMidnite said:
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.

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.

http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/applet.html

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.
 
Mark44 said:
Have you written a main method? That's what the error is telling you.

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.
 
  • #10
AlephZero said:
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.
Thanks, I didn't remember that - it's been 17 years since I wrote any Java applets, so I'm a bit rusty.
AlephZero said:
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
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:
<HTML>

<HEAD>

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

</HEAD>

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

</BODY>

</HTML>

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.
 
  • #12
SolarMidnite said:
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:
<HTML>

<HEAD>

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

</HEAD>

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

</BODY>

</HTML>

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.

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.
 
  • #13
chiro said:
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.

Thank you so much, chiro! Removing the quotes from "Triangle.class" solved the problem. I really appreciate everyone who helped me through the problem.
 

Similar threads

Back
Top