Friday, March 2, 2012

java.lang.NoSuchMethodError: main Exception in thread "main"

How to solve java.lang.NoSuchMethodError: main Exception in thread "main"

java.lang.NoSucMethodError comes when Java code tries to call a method which is not existed on a class, this could be either static or non static method. most common manifestation of java.lang.NoSuchMethodError is running a class which doesn't have main method in Java. In this article we will see what is "java.lang.NoSuchMethodError: main Exception in thread "main"" , Why does java.lang.NoSuchMethodError comes and how to solve java.lang.NoSuchMethodError in Java.

What is "java.lang.NoSuchMethodError: main Exception in thread "main""

java.lang.NoSuchMethodError: main Exception in thread This is a manifestation of NoSuchMethodError in Java and complaining about "main" which is name of method JVM tries to call but doesn't find in offending class. "java.lang.NoSuchMethodError: main Exception in thread "main" can come due to various reasons like:

1) Class which you are trying to run doesn't have main method.
2) Signature of main method is not correct . See here for all possible signature of main method in Java.

Example of "java.lang.NoSuchMethodError: main Exception in thread "main"

To better understand NoSuchMethodError in Java let's see some common examples of java.lang.NoSuchMethodError and find out how and when they come:

1) try to run a java class which doesn't have a main method:

public class Loan{
  public String getPersonalLoan(){
        return "Personal Loan";
  };
}

2) Now compile this class it should compile without any error:

user@home:~/java javac Loan.java


3) Run  as "java Loan"

user@home:~/java java Loan
java.lang.NoSuchMethodError: main
Exception in thread "main"

There are very few scenarios on which java.lang.NoSuchMethodError come during runtime because compiler is able to check this error and flag it if doesn't found the proper method except main which is optional method and only checked by JVM during runtime. As I said earlier Another possible cause of "java.lang.NoSuchMethodError: main Exception in thread "main" is that you have main method in class but signature of main method is not correct. to test this scenario let's
add a main method in Loan.java

public class Loan{
  public String getPersonalLoan(){
        return "Personal Loan";
  };

  public static void main(String args){
     System.out.println("Inside main in Java");
  }
}


Now compile and run this Java program. You will still get:

java.lang.NoSuchMethodError: main Exception in thread "main"

because in main method args should be string array and we have not added [] there and that's why this error.


That’s all on How to fix java.lang.NoSuchMethodError: main Exception in thread "main . Ideally java.lang.NoSuchMethodError comes when we call a method which doesn’t exits in class but mostly typo, incorrect argument or incorrect signature turns out to be real culprit.

Other Java Error Exception tutorial you may like:

4 comments :

Siddhu said...

I didn't understand point that why would someone want to execute a class without main ? Though error is quite clear "java.lang.NoSuchMethodError: main Exception in thread "main" , first part says that there is no main method in this class "java.lang.NoSuchMethodError: main" and second part says that Exception is occurred in main thread.

manish sehgal said...

class Dog{
int size;
String name;
void bark()
{
if(size>70)
{System.out.println("bhoo bhoo");
}
else if(size>14)
{
System.out.println("foo foo");
}
else
{
System.out.println("yip yip");
}
}
}
class Dogtestdrive{
public static void main(String[] args)
{
Dog one = new Dog();
one.size=80;
Dog two = new Dog();
two.size=20;
Dog three = new Dog();
three.size=12;
one.bark();
two.bark();
three.bark();
}
}
i get the same error...can anyone tell me where is the error?

uday arun said...

the following is my code and i am getting same error can you please correct it??






import java.lang.*;
import java.util.*;

class Core implements Runnable{
String name;
Thread t;
Core(String n){
name=n;
t=new Thread(this ,n);
System.out.print("thread name"+t);
t.start();
}
public void run(){
try{
for(int i=0;i<5;i++){
System.out.print(name+" : "+i);
Thread.sleep(1000);}
}catch(InterruptedException e)
{System.out.print(e);
}
System.out.print(" main thread exiting ");
}
}

class Series {
public static void main(String arun[]){
new Core("one");
new Core("two");
new Core("three");
try{
Thread.sleep(1000);
}catch(InterruptedException e)
{
System.out.print("main thread interrrupted"+e);
}
}
}

Anonymous said...

make two files one with class Dog, the other file with the other class.

Post a Comment