tags:

views:

131

answers:

4

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?

+1  A: 

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
+2  A: 

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
A: 

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 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
The answer may be wrong, but it doesn't suggest a deep misunderstanding. Whitespace is important.
Adrian Mouat
@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
i agree with Chris
David
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
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
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
@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
@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
A: 

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