Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception in thread "main" java.lang.NoSuchMethodError: main [duplicate]

Tags:

java

exception

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?

like image 733
Ramesh Avatar asked Dec 17 '22 22:12

Ramesh


2 Answers

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.
like image 65
BalusC Avatar answered May 03 '23 19:05

BalusC


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

like image 22
NullUserException Avatar answered May 03 '23 21:05

NullUserException