Selasa, 08 April 2014

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:



































Source:http://javarevisited.blogspot.com/2012/03/javalangnosuchmethoderror-main.html

Tidak ada komentar:

Posting Komentar