#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Waukesha, Wisconsin
    Posts
    131
    Rep Power
    16

    Program compiles fine, but crashes upon execution


    Hi all..I have a program that compiles fine, but when I try to run it, it comes back with the error: Exception in thread "main" java.lang.NoSuchMethodError: main Can someone please explain to me what that means? I have no idea where to begin to try to solve it. I can include my source code if you need to review it.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    greece
    Posts
    43
    Rep Power
    16
    give the source to the community mate
  4. #3
  5. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,419
    Rep Power
    1717
    Originally Posted by chm0d
    give the source to the community mate
    ... and tell us how you are running it. Not finding main could be a problem with how you are running the program too.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Waukesha, Wisconsin
    Posts
    131
    Rep Power
    16

    Program compiles fine, but crashes upon execution


    Sorry about that. I wasn't sure if it was a generic enough of an error that wouldn't need it..anyway, here it is..the source file is too big to include here, so I'll include it as an attachment. I hope that by doing a copy of it from a *.java file to *.txt it didn't loose its formatting.
    Attached Files
  8. #5
  9. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,419
    Rep Power
    1717
    A normal Java application looks something like:

    Code:
    public class SomeClass
    {
    // member variable defs
    
        public static void main( String argv[] )
        {
        }
    }
    You will need that main method with the signature shown to be able to run a Java application from the command line. The main method may call static methods in the class (as it itself is static) or you may just want to create a new SomeClass and call methods on that object.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Waukesha, Wisconsin
    Posts
    131
    Rep Power
    16
    I'm not sure what you mean..I am new to Java programming.
  12. #7
  13. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,419
    Rep Power
    1717
    Sorry, I looked at your code again and I see the issue:

    Code:
    public void main (String[] arguments) throws IOException 
    { 
        usd_Menu();
    }
    
    public void usd_Menu() throws IOException
    To have a java application your main() method must be declared as

    Code:
    public static void main (String[] arguments) throws IOException
    Next, because main() is a static method you can't call non-static methods. So you have two choices. The first would be to make everything (member variables and methods) static. However, this is not a great long term solution so the other method would be to change your main to be something like:

    Code:
    public static void main (String[] arguments) throws IOException
    {
        CurrencyConv1 cc1 = new CurrencyConv1();
    
        cc1.usd_Menu();
    }

    Does that make sense?

    Comments on this post

    • StevenC agrees
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Waukesha, Wisconsin
    Posts
    131
    Rep Power
    16
    Thank you. I think I understand now much better and it works too!
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Waukesha, Wisconsin
    Posts
    131
    Rep Power
    16

    Smile Program compiles fine, but crashes upon execution


    I added the logic as suggested, but have another question for you..Is there some trick to defining more than one additional method than the USD_Menu that I have added so far? I have approximately 23 more to add.
  18. #10
  19. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,419
    Rep Power
    1717
    I'm not positive I follow exactly what you're asking but Java allows you to overload methods. This means that you could have something like:

    Code:
    public void usd_Menu() throws IOException
    
    // and
    
    public void usd_Menu(String blah) throws IOException
    
    // and
    
    public void usd_Menu(String blah, int blech) throws IOException
    and so on. Be careful though - you cannot overload a method when only the return value changes. That is:

    Code:
    public void usd_Menu() throws IOException
    
    // and
    
    public int usd_Menu() throws IOException
    is not allowed as only the return value has changed.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    New York
    Posts
    328
    Rep Power
    16
    You can also do:

    Code:
    public void usd_Menu(String blah) throws IOException
    
    // and
    
    public void usd_Menu(int blech) throws IOException
    or
    Code:
    public void usd_Menu(String blah) throws IOException
    
    // and
    
    public void usd_Menu2(String blah) throws IOException
    If you think I helped~ me~~

  22. #12
  23. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Waukesha, Wisconsin
    Posts
    131
    Rep Power
    16
    Thanks for the help. These will be great sources to refer to.

IMN logo majestic logo threadwatch logo seochat tools logo