something wrong in code?

  • Hiei
  • Beginner
  • Beginner
  • Hiei
  • Posts: 51

Post 3+ Months Ago

im starting to learn java and i have a practice code, which draws a diagnle line but when i run it there is an error which is
Code: [ Select ]
Exception in thread "main" java.lang.NoSuchMethodError: main

and the code i have is this

Code: [ Select ]
import java.awt.Graphics;

public class FirstApplet extends java.applet.Applet
{

  public void paint(Graphics g)
  {
    g.drawLine(0, 0, 200, 200);
  }
}
  1. import java.awt.Graphics;
  2. public class FirstApplet extends java.applet.Applet
  3. {
  4.   public void paint(Graphics g)
  5.   {
  6.     g.drawLine(0, 0, 200, 200);
  7.   }
  8. }
i am new to java so this could be a n00b mistake but will someone help me
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

If it's telling you that it can't find a "main" then I suspect that you are trying to invoke it as an application rather than as an applet. I don't see any error in the code and it ran fine for me in appletviewer and the browser. After you compile it and have the FirstApplet.class file, make a simple webpage with an applet tag and invoke appletviewer or open the page in a java-enabled browser and it should run. Don't try to run it with java FirstApplet from the command line or from the run button of whatever ide tool you're using or else it will run as an application and the runtime environment will look for the main function as the entry point.

Here's the test.html file I used to test it:

Code: [ Select ]
<html>
<head>
</head>
<body bgcolor="#000000">
<center>
<applet code = "FirstApplet.class" width = "500" height = "300">
</applet>
</center>
</body>
</html>
  1. <html>
  2. <head>
  3. </head>
  4. <body bgcolor="#000000">
  5. <center>
  6. <applet code = "FirstApplet.class" width = "500" height = "300">
  7. </applet>
  8. </center>
  9. </body>
  10. </html>


And then I ran it from the command line with appletviewer test.html and of course you can just open the page in a java-enabled browser.
  • Hiei
  • Beginner
  • Beginner
  • Hiei
  • Posts: 51

Post 3+ Months Ago

ok then it might be what im using to compile it. im using something called JJedit to compile and run my code. is there something else i can use to run it. i tried the command line and it says i have 11 errors when i run that. thanks for the help anyways
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I don't know why it would tell you there are 11 errors. To get it to run I just copied and pasted the code example you posted into notepad, saved it as "FirstApplet.java" on my desktop, made the html file and saved it to the desktop as "test.html", and then from the command line I compiled and ran it with the commands:

C:\WINDOWS\Desktop>javac FirstApplet.java

C:\WINDOWS\Desktop>appletviewer test.html

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 53 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.