4

Im having trouble with a simple hello world program lol! Im hoping someone can shed some light on this.

So the error im receiving is the following:

$ javac Hello.java
$ java Hello
Exception in thread "main" java.lang.NoSuchMethodError: main

So by the error, I can see that it's obviously missing main, but it's there:

class Hello
{
  public static void main(String argv)
  {
    System.out.println("hello world");
  }
}

I'm on Mac OS/X if it's any help.

Menztrual
  • 40,867
  • 12
  • 57
  • 70

7 Answers7

10

Problem is that your method does not take String array as a argument. Use following signature instead:

public static void main(String[] argv)

or

public static void main(String argv[])

Other valid option is:

public static void main(String ... argv)

In Java Language Specification this is told as follows:

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • You do not have to give it any actual arguments when starting JVM. In such a case main method will receive empty array. – Mikko Maunu Jul 30 '12 at 11:34
  • Just a side question; Im still receiving the error by removing the args part to just this: `public static void main()` – Menztrual Jul 31 '12 at 00:54
  • 1
    That's how it is, main simply needs to have array as an argument. But you do not have to give arguments when you execute it with 'java Hello'. – Mikko Maunu Jul 31 '12 at 04:40
4

You forgot about [] in String[] argv or ... in String... argv. This array is used to store arguments used in command creating JVM for your class like

java Hello argument0 argument1 argument2` 

and so on.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
4

Main method has signature that accepts String[] and you wrote String which is wrong,

Make it

public static void main(String[] argv)

or varargs

public static void main(String... argv)
Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
4

You forgot to put the array syntax, You can even use varargs as per JAVA 1.5

public static void main(String... argv)
code-jaff
  • 9,230
  • 4
  • 35
  • 56
2

problem is with main signature, which should be

public static void main(String[] argv)

or could be

public static void main(String ... argv) // known as varargs

instead of public static void main(String argv) which is in your case

have a look at this

for varargs look

Harmeet Singh
  • 2,555
  • 18
  • 21
2
public static void main(String[] args)
public static void main(String... args)
public static void main(String args[])

Java programs start executing at the main method, which has the above method prototype

swapy
  • 1,616
  • 1
  • 15
  • 31
1

Your main method signature is wrong String instead of String []

use

public static void main(String[] argv)

or

public static void main(String... argv)

read here

subodh
  • 6,136
  • 12
  • 51
  • 73