4

Possible Duplicate:
Exception in thread “main” java.lang.NoSuchMethodError: main

I got the above message. The code is as follows:

class Test
{
 public static void main(String ar[])
 {
  printf("hai");
 }
}

How is this problem caused and how can I fix it?

Community
  • 1
  • 1
Ramesh
  • 41
  • 1
  • 1
  • 2
  • I'm confused, how did you even compile it? My test: `Sandbox.java:27: cannot find symbol symbol : method printf(java.lang.String)` – TheLQ Aug 22 '10 at 04:31
  • 1
    @Lord: Which only confirms my suspicion that he isn't executing the class he think he is executing :) – BalusC Aug 22 '10 at 04:36
  • http://stackoverflow.com/questions/5407250/causes-of-java-lang-nosuchmethoderror-main-exception-in-thread-main – Stephen C Nov 23 '11 at 06:51

4 Answers4

6

The class which you're trying to execute doesn't have a main method.

Since your main method looks syntactically fine, this can have two causes:

  1. You're executing the wrong class.
  2. The actual class file doesn't contain this code.

The solution is obvious:

  1. Make sure that your command is pointing the correct class file, you might have multiple class files with the same name and be sitting in the wrong directory.
  2. Make sure that you've compiled the correct source file into the correct class file before, you might have edited one and other and forgot to recompile.
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
4

In addition to the problem that's causing the current exception (see BalusC's answer), the proper "Hello World" in Java is:

class Test
{
    public static void main(String[] args) {
        System.out.println("hai");
    }
}

See: java.lang.System

NullUserException
  • 77,975
  • 25
  • 199
  • 226
  • That would only have caused a **different** exception *when* he got the `main` to run. – BalusC Aug 22 '10 at 04:11
  • The normal convention is by the way to put braces on type declatation :) – BalusC Aug 22 '10 at 04:26
  • 1
    From the basic [arrays tutorial](http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html): *However, convention discourages this form; the brackets identify the array type and should appear with the type designation.* – BalusC Aug 22 '10 at 04:47
  • @Balus Oh NVM, I thought you meant the curly braces. on `class Test {`. I just copy and pasted the OP's code, didn't change the brackets. Answer edited. – NullUserException Aug 22 '10 at 04:49
1

I see your problem, the signature is not correct. It should be public static void main(String[] args)

LLS
  • 1,968
  • 2
  • 20
  • 32
1

It could also be a class path issue which causes Eclipse to get confused and not able to find your class when it tries to run it. I would look at the Java Build Path in the Project Properties to make sure there are no errors.

Jay Askren
  • 9,793
  • 13
  • 48
  • 73