Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java Eclipse Code Error

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Eclipse Code Error

    Hello. I'm very new to Java, and I have just recently found a tutorial that I am using. I cut-and-pasted a bit of code -- some code that I saw the tutorial use successfully from the command line -- from the tutorial's website into Eclipse. Here is the code:

     
    import java.awt.Canvas;
    import java.awt.Graphics;
    import java.awt.FontMetrics;
    import java.awt.Rectangle;
     
    public class AdvancedWindowClass2 extends Canvas {
        AdvancedWindowClass2() {
            setSize(200,200);
        }
        public void paint(Graphics g) {
            Rectangle rect;
            rect = getBounds();
            String str;
            str = "I'm in the center!";
            FontMetrics fm = g.getFontMetrics();
            int strwidth = fm.stringWidth(str);
            int y = rect.height / 2;
            int x = (rect.width / 2) - (strwidth / 2);
            g.drawString(str,x,y);
        }
    }

    Eclipse didn't show any errors, so I ran it, and got this error:

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

    Being new to coding and Java, I have no idea what this means, although my guess would be that Eclipse is searching my code for a "main" method, and not finding it. However, the code that I pasted in didn't have a "main" method, and it worked fine in the tutorial, though that was from the command line. Does that have something to do with the error? If anyone knows the problem, I would really appreciate some help!

    Thanks!

    --- Update ---

    Sorry. That first sentence with a bit confusing. What I meant was that I have just found a tutorial for the Java programming language, and I am using it to learn Java.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,122
    My Mood
    Mellow
    Thanks
    186
    Thanked 658 Times in 645 Posts

    Default Re: Java Eclipse Code Error

    Your guess about the error message is a good one. Yes, a main() method is needed as the starting point for a Java class to be runnable/stand alone, though Applets are slightly different animals. However, this isn't an Applet. There were "things" going on behind the scenes in the tutorial you watched (I assume), and the piece of code you copied and pasted into Eclipse was a small piece of a larger whole.

    Have you done the basic HelloWorld tutorial/trail to learn the basic parts of a Java program? You can follow the Oracle version and adapt as needed to the Eclipse IDE, or you could do the Eclipse tutorials that are available from the Welcome screen. You can get Eclipse's Welcome screen by selecting "Help" then "Welcome".

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    CrazyCoder (July 28th, 2013)

  4. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Eclipse Code Error

    Quote Originally Posted by GregBrannon View Post
    Your guess about the error message is a good one. Yes, a main() method is needed as the starting point for a Java class to be runnable/stand alone, though Applets are slightly different animals. However, this isn't an Applet. There were "things" going on behind the scenes in the tutorial you watched (I assume), and the piece of code you copied and pasted into Eclipse was a small piece of a larger whole.

    Have you done the basic HelloWorld tutorial/trail to learn the basic parts of a Java program? You can follow the Oracle version and adapt as needed to the Eclipse IDE, or you could do the Eclipse tutorials that are available from the Welcome screen. You can get Eclipse's Welcome screen by selecting "Help" then "Welcome".
    Thanks! I followed your advice and went and watched the tutorial again, and realized that the particular thing I was doing required another class (I think that's what it's called) in order to function. I added this other class, and sure enough, it worked! Thanks again! I really appreciate it!

Similar Threads

  1. How to link .java code in eclipse to tomcat
    By shenaz in forum Java Theory & Questions
    Replies: 0
    Last Post: April 1st, 2013, 11:54 AM
  2. What is wrong with my code? JAVA-Eclipse
    By bespinoz in forum Loops & Control Statements
    Replies: 9
    Last Post: November 17th, 2012, 03:06 PM
  3. Error Running Java in Eclipse
    By steadyonabix in forum Java IDEs
    Replies: 1
    Last Post: September 3rd, 2011, 10:28 PM
  4. Java Tip Jul 5, 2010 - [Eclipse IDE] Navigating through code
    By helloworld922 in forum Java JDK & IDE Tutorials
    Replies: 1
    Last Post: July 5th, 2010, 06:28 AM
  5. Java Tip Jul 5, 2010 - [Eclipse IDE] Navigating through code
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: July 5th, 2010, 06:28 AM

Tags for this Thread