Java Applet Issues: Solve NoSuchMethodError

  • Java
  • Thread starter SolarMidnite
  • Start date
  • Tags
    Issues Java
In summary, the Triangle class doesn't have a main method and the error you are getting is because you aren't including it in your Triangle.java file. You will need to include this in order for the applet to run.
  • #1
SolarMidnite
22
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
  • #2
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.
 
  • #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:

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!
 
  • #4
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.
 
  • #6
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.
 
  • #7
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.
 
  • #8
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.
 
  • #9
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.
 

FAQ: Java Applet Issues: Solve NoSuchMethodError

What is a NoSuchMethodError in Java applets?

A NoSuchMethodError is an error that occurs when a Java applet cannot find a method that it is trying to use. It typically means that the applet is using an outdated or incorrect version of a method, or that the method has been removed or renamed.

How can I solve a NoSuchMethodError in my Java applet?

To solve a NoSuchMethodError, you should first check that you are using the correct version of the method. If the method has been renamed or removed, you will need to update your code to use the new method. You should also make sure that all necessary libraries and dependencies are included in your applet.

Why am I getting a NoSuchMethodError when my applet was working before?

If your applet was working before but is now giving a NoSuchMethodError, it is likely that something has changed in your environment. This could be a change in the version of Java or a change in the libraries or dependencies being used. Make sure to check for any updates or changes that may have caused the error.

Can a NoSuchMethodError be caused by a typo?

Yes, a NoSuchMethodError can be caused by a typo in your code. If you have misspelled a method name or used the wrong case, the applet will not be able to find the method and will throw the error. Always double check your code for typos to avoid this issue.

Is there a way to prevent NoSuchMethodErrors in Java applets?

While it is not always possible to prevent NoSuchMethodErrors, there are some steps you can take to minimize the likelihood of encountering this issue. These include keeping your code and libraries up-to-date, avoiding typos, and thoroughly testing your applet before deployment.

Similar threads

Replies
1
Views
1K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
4
Views
4K
Replies
9
Views
3K
Back
Top