Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    ravifedora is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

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

    Hi,

    I get the "Exception in thread "main" java.lang.NoSuchMethodError: main" while executing my code.


    I have two packages. I am inheriting from a class from one of the packages. Here's the code:

    package food;
    public class Fruit{
    protected void printFruit() {
    System.out.println("aatukutti is too much to handle.");
    }

    public void main(String args[])
    {
    }
    }



    import food.Fruit;
    class Apple extends Fruit{

    public void main(String args[]) {

    Apple ap= new Apple();
    ap.printFruit();
    }
    }

    The code compiles fine. And when I execute it,I get the java.lang.NoSuchMethodError: main. Could you please help me out?

    Thanks,

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Non static methods can't be accessed unless you create an instance of that class. Your main method is missing something as the exception implies. It can't find the main method

  3. #3
    ravifedora is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    Non-static methods can be accessed directly from classes inheriting them (without having to instantiate the super class). Well, both my packages have main methods. I really don't know whats missing.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    First you have a blank main method in one of the classes. Not all classes need a main method, having a blank one is unnecessary.

    Other than that just examine how your main method is constructed. The first thing the jvm does is call main, what is necessary for it to directly call that method? Google a hello world example and look at there main method if you are still unsure.

  5. #5
    ravifedora is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    I can't think of any reason why either of my main method declarations is wrong.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    :headdesk:

    STATIC

  7. #7
    ravifedora is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    I gave static keyword. But the program won't compile mentioning that I cannot override from original packages. So I removed static from the main declaration - I think thats harmless.

  8. #8
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    public static void main(String args[]){
    }
    sanjeev,संजीव

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Here is my hello world application. Tell me shaw different.

    Java Code:
    public class Hello{
      public static void main(String[] arg){
        System.out.println("hello, world");
      }
    }

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by ravifedora View Post
    I gave static keyword. But the program won't compile mentioning that I cannot override from original packages.
    Then you have an error in your code which must be fixed. The main method MUST BE static.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also try making the fruit class have package access and making the apple class public if they are in the same file. Change the file name to Apple.java as well.

  12. #12
    ravifedora is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    Like I said the only keyword that is missing in the main() declaration is the STATIC keyword. I think thats harmelss.

  13. #13
    ravifedora is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    Hi sunde,

    they are in 2 different files named Fruit.java and Apple.java. Both of them compile fine. But I am not able to execute it.

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ok, what happens when you type java Apple in cmd?

    Also, like others said, ALL main methods should be static.

  15. #15
    ravifedora is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    Alright guys, I added the "STATIC" keyword. Program compiles fine.

    When I do java Apple . I get "Exception in thread "main" java.lang.NoSuchMethodError: main"

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Did you put static, or STATIC? it should be the former not the latter.

  17. #17
    ravifedora is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    i put static (lowercase). the program would not have compiled otherwise, you know it :)

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Post all the code again please.

  19. #19
    ravifedora is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default

    Hi Sunde,

    here is the modified version:

    Fruit.java
    --------------


    package food;
    public class Fruit{
    protected void printFruit() {
    System.out.println("aatukutti is too much to handle.");
    }
    }


    Apple.java
    ---------------
    import food.Fruit;
    class Apple extends Fruit{
    public static void main() {
    Apple ap= new Apple();
    ap.printFruit();
    }
    }

    I compile Fruit.java first and then Apple.java. Both compiles successfully. I do java Apple and get the aforementioned error.

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try doing Fruit ap = new Apple(); and tell me what happens.

    If it still throws the exception paste the entire exception.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •