• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding method calling

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

If I have a main method in Java which returns int
public static int main(String args[])
then at the time of compiling the program we don�t get any compilation error, but at run time why a java.lang.NoSuchMethodError occurs?

My another question is also similar with the above one.
Let say I have 2 class, one is TestClass1.java and TestClass2.java

// TestClass1
class TestClass1
{
public static void main(String[] args)
{
TestClass2 testClass2 = new TestClass2();
testClass2.testMethod();
}
}

// TestClass2
class TestClass2
{
public void testMethod()
{
System.out.println("I am in testMethod of TestClass2");
//return 1;
}
}

We are calling testMethod() of TestClass2 from TestClass1. After compiling these 2 program if I execute the TestClass1 program then it runs perfectly. Now if I change the return type of testMethos() and compile only TestClass2 not the TestClass1 and run TestClass1 then an exception is thrown from main that is also java.lang.NoSuchMethodError. Is this related with the previous problem any more?

Regards,
Gourab
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

case 1:
The compiler just checks the syntax errors during compilation along with any references, imports etc. It does not find any mistake. So it passes the compilation. According to compiler "public static int main(String[] args)" is a perfectly legal and valid method. It does not know and care about the running of those methods during compilation.

But while running, the JVM exactly needs for a method matching with the signature of "public static void main(String[] args)" which incase is absent. So why the "java.lang.NoSuchMethodError" is given.

Case 2:
Here eventhough you had changed the TestClass2 and compiled it again, while running TestClass1 it will take the .class file which was just now overridden because of your recent compilation on TestClass2. In this case the actual method referred in TestClass1 is found missing so why the same error again.

HtH.
reply
    Bookmark Topic Watch Topic
  • New Topic