Results 1 to 20 of 29
- 03-16-2011, 03:45 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
Exception in thread "main" java.lang.NoSuchMethodError: main
Hi,
I get the "Exception in thread "main" java.lang.NoSuchMethodError: main" while executing my code.
I have two packages. I am inheriting from a class from one of the packages. Here's the code:
package food;
public class Fruit{
protected void printFruit() {
System.out.println("aatukutti is too much to handle.");
}
public void main(String args[])
{
}
}
import food.Fruit;
class Apple extends Fruit{
public void main(String args[]) {
Apple ap= new Apple();
ap.printFruit();
}
}
The code compiles fine. And when I execute it,I get the java.lang.NoSuchMethodError: main. Could you please help me out?
Thanks,
- 03-16-2011, 03:51 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Non static methods can't be accessed unless you create an instance of that class. Your main method is missing something as the exception implies. It can't find the main method
- 03-16-2011, 05:10 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
Non-static methods can be accessed directly from classes inheriting them (without having to instantiate the super class). Well, both my packages have main methods. I really don't know whats missing.
- 03-16-2011, 05:18 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
First you have a blank main method in one of the classes. Not all classes need a main method, having a blank one is unnecessary.
Other than that just examine how your main method is constructed. The first thing the jvm does is call main, what is necessary for it to directly call that method? Google a hello world example and look at there main method if you are still unsure.
- 03-16-2011, 05:23 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
I can't think of any reason why either of my main method declarations is wrong.
- 03-16-2011, 05:25 AM #6
:headdesk:
STATIC
- 03-16-2011, 05:27 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
I gave static keyword. But the program won't compile mentioning that I cannot override from original packages. So I removed static from the main declaration - I think thats harmless.
- 03-16-2011, 05:28 AM #8
public static void main(String args[]){
}sanjeev,संजीव
- 03-16-2011, 05:29 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Here is my hello world application. Tell me shaw different.
Java Code:public class Hello{ public static void main(String[] arg){ System.out.println("hello, world"); } }
- 03-16-2011, 05:30 AM #10
- 03-16-2011, 05:30 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Also try making the fruit class have package access and making the apple class public if they are in the same file. Change the file name to Apple.java as well.
- 03-16-2011, 05:31 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
Like I said the only keyword that is missing in the main() declaration is the STATIC keyword. I think thats harmelss.
- 03-16-2011, 05:32 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
Hi sunde,
they are in 2 different files named Fruit.java and Apple.java. Both of them compile fine. But I am not able to execute it.
- 03-16-2011, 05:35 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Ok, what happens when you type java Apple in cmd?
Also, like others said, ALL main methods should be static.
- 03-16-2011, 05:37 AM #15
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
Alright guys, I added the "STATIC" keyword. Program compiles fine.
When I do java Apple . I get "Exception in thread "main" java.lang.NoSuchMethodError: main"
- 03-16-2011, 05:39 AM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Did you put static, or STATIC? it should be the former not the latter.
- 03-16-2011, 05:40 AM #17
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
i put static (lowercase). the program would not have compiled otherwise, you know it :)
- 03-16-2011, 05:41 AM #18
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Post all the code again please.
- 03-16-2011, 05:53 AM #19
Member
- Join Date
- Mar 2011
- Posts
- 14
- Rep Power
- 0
Hi Sunde,
here is the modified version:
Fruit.java
--------------
package food;
public class Fruit{
protected void printFruit() {
System.out.println("aatukutti is too much to handle.");
}
}
Apple.java
---------------
import food.Fruit;
class Apple extends Fruit{
public static void main() {
Apple ap= new Apple();
ap.printFruit();
}
}
I compile Fruit.java first and then Apple.java. Both compiles successfully. I do java Apple and get the aforementioned error.
- 03-16-2011, 06:02 AM #20
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM -
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks