0

when i try to compile this:

public class Risk
{
}
class territory 
{

    public static void main (String[]arg) 
    {
        System.out.println ("hi") ; 
    } 
}

I get this error message:

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

whats going wrong here?

David
  • 13,509
  • 33
  • 71
  • 101
  • This Community Wiki question lists the possible causes of this common problem: http://stackoverflow.com/questions/5407250/causes-of-java-lang-nosuchmethoderror-main-exception-in-thread-main – Stephen C Jun 28 '11 at 14:37

5 Answers5

3

The class containing the main() function must be public, and you may only define one public class per file. You'll want to have two separate files Risk.java and Territory.java.

Risk.java:

public class Risk {
}

Territory.java:

public class Territory 
{

    public static void main (String[]arg) 
    {
        System.out.println ("hi") ; 
    } 
}

EDIT: It turns out this isn't true - I was able to run your initial code with the following command line:

java territory

But my earlier comments point to the best practice for a real app, such as a Risk game.

Benjamin Cox
  • 5,880
  • 18
  • 19
1

What class are you trying to run? If you're using the class territory, that will work. Risk has no main method, though.

Chris Jester-Young
  • 206,112
  • 44
  • 370
  • 418
0

Can you figure out why this example causes the same issue?

public class Simple {
    public void main(String args[]) {
        System.out.println("Inside function");
    }
}

Answer: because main() should be public static void!

Amil
  • 609
  • 1
  • 5
  • 12
-1

Could it just be a spacing issue? Your original post shows no space between the ']' and 'arg'.

Try this:

public static void main (String[] arg) 

or, if that still doesn't work:

public static void main (String arg[]) 
elduff
  • 1,138
  • 3
  • 13
  • 22
  • 4
    -1 No, both of those are immaterial considerations. (Normally I don't downvote posts, but ones that suggest that formatting changes will make any difference demonstrate a deep misunderstanding of the language.) – Chris Jester-Young Mar 22 '10 at 22:03
  • The answer may be wrong, but it doesn't suggest a deep misunderstanding. Whitespace is important. – Adrian Mouat Mar 22 '10 at 22:25
  • 1
    @Adrian: Sometimes, yes. (`class Foo` is different from `classFoo`.) Other times, such as here, no. Knowing when it makes a difference is a sign of a competent programmer. – Chris Jester-Young Mar 22 '10 at 22:44
  • 2
    People don't need to make excuses for down-voting a blatantly wrong answer. Down-voting incorrect answers encourages people to verify their facts before posting. – Stephen C Mar 23 '10 at 03:51
  • I don't disagree with down-voting the answer (it was wrong). I just feel that suggesting the answerer has a deep misunderstanding is a bit strong. – Adrian Mouat Mar 23 '10 at 09:31
  • Yeah, I missed the the main issue there (it's unclear which class the poster was trying to call). And, I see now that my answer is incorrect, but disagree with the 'deeper misunderstanding' comment. Whitespace is important, as is formatting for readability/best practices (if not for compilation in this case). – elduff Mar 23 '10 at 14:45
  • 1
    @elduff: It's one thing to say formatting is good for readability. It's another to say it changes language semantics. (In a language like Python, formatting does indeed change semantics, but we're talking about Java here.) Being a good programmer means you're able to tell which is which. – Chris Jester-Young Mar 23 '10 at 15:13
  • 1
    @Adrian: Depends on how you define "deep misunderstanding". To me, if one doesn't understand how code is tokenised in a given language (i.e., in Java, `String[]args` is tokenised the same as `String [ ] args`), that says something about how much they understand the language. – Chris Jester-Young Mar 23 '10 at 15:21
-1

What the answer wound up being was that the class i run must contain main or else it won't work. i'm posting this because, though other answers give roughly the same information, they don't make it explicit.

David
  • 13,509
  • 33
  • 71
  • 101