Exception in the topic "main" java.lang.NoSuchMethodError: main

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 

What's going on here?

0
source share
5 answers

That the answer ended is that the run class I should contain main , otherwise it will not work. I am posting this because, although other answers give roughly the same information, they do not make it explicit.

0
source

The class containing the main() function must be public, and you can define only one public class for each file. You 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 that this is not true - I was able to run your source code using the following command line:

 java territory 

But my previous comments point to best practice for a real application, for example, for the game Risk.

+3
source

What class are you trying to run? If you use the territory class, this will work. Risk does not have a main method.

+1
source

Can you understand why this example is causing the same problem?

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

Answer: because main () must be publicly available static void!

0
source

Could this just be a question? Your original message does not show a space between ']' and 'arg'.

Try the following:

 public static void main (String[] arg) 

or if this still does not work:

 public static void main (String arg[]) 
-1
source

All Articles