How to solve java.lang.NoSuchMethodError: main Exception in thread "main"
java.lang.NoSucMethodError comes when Java code tries to call a method that does not exist on a class, this could be either static or non-static method. a most common manifestation of java.lang.NoSuchMethodError is running a class that doesn't have the 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""
This is a manifestation of NoSuchMethodError in Java and complaining about "main" which is the 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) The class which you are trying to run doesn't have the main method.
2) The signature of the main method is not correct. See here for all possible signatures of the 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 that doesn't have a main method:
public class Loan{
public String getPersonalLoan(){
return "Personal 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"
java.lang.NoSuchMethodError: main
Exception in thread "main"
There are very few scenarios on which java.lang.NoSuchMethodError comes during runtime because the compiler is able to check this error and flag it if doesn't found the proper method except main which is an 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 the main method in class but the signature of the main method is not correct. to test this scenario let's
add the 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");
}
}
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 the main method args should be a 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 that doesn’t exist in class but mostly typo, incorrect argument or incorrect signature turns out to be the real culprit.
Other Java Error Exception tutorial you may like:
Let me know if you still not able to solve this problem and I will try to help you as much possible in comments.
5 comments :
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.
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?
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);
}
}
}
make two files one with class Dog, the other file with the other class.
This is my program which is generating the same erroe
Please help me to find out the error
import java.io.*;
class A
{
public static void main(String args[])throws IOException
{
int i;
FileInputStream fin=null;
FileOutputStream fout=null;
try
{
fin=new FileInputStream (args[0]);
}
catch(FileNotFoundException temp)
{
System.out.println("Exception caught"+temp);
}
try
{
fout=new FileOutputStream (args[1]);
}
catch(FileNotFoundException temp)
{
System.out.println("Exception caught"+temp);
}
catch(ArrayIndexOutOfBoundsException ai)
{
System.out.println(ai);
}
try
{
do
{
i=fin.read();
if(i!=1)
fout.write(i);
}
while(i!=1);
}
catch(IOException io)
{
System.out.println("Exception caught");
}
fin.close();
fout.close();
}
}
Post a Comment