This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes getting a java.lang.NoSuchMethodError: main Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "getting a java.lang.NoSuchMethodError: main" Watch "getting a java.lang.NoSuchMethodError: main" New topic
Author

getting a java.lang.NoSuchMethodError: main

yadav mayank
Greenhorn

Joined: Jan 12, 2010
Posts: 7
hi
i just wrote my first program in Java and got that error.
please help , what should i do.


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.

And welcome to JavaRanch!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35443
    
    9
How are you trying to run this code? Class PracticeTest should be public, and then you'd enter "java PracticeTest" on the command line.


Android appsImageJ pluginsJava web charts
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
Welcome to JavaRanch Have you got your problem sorted yet?
yadav mayank
Greenhorn

Joined: Jan 12, 2010
Posts: 7
thanks frieds
i made Practice public but since my main was in PracticeTest and i was compiling : java Practice , i was getting that error.
my mistake
it should be compiled as : java PracticeTest.

but again i have a query , why should class PublicTest be public?
i made Practice public and compiled and ran using java PracticeTest....isnt it proper?
Efrain Morales
Greenhorn

Joined: Oct 14, 2009
Posts: 12
Because the class that has the main method has to be accessible from the outside.
yadav mayank
Greenhorn

Joined: Jan 12, 2010
Posts: 7
Thanks a lot friends for patiently replying to all my naieve questions.
Just one final question.
Tell me which of my understanding is true :

1)one java file can have as many classes as we want but only one class can be public and that should have main method also the name of the java file will be the name of the public class ie the class which has main.

2)one java file can have as many classes as we want and also as many public classes as we want and out of these public classes one should hav the "main" and the name of the java file will be the nameof the public class having main ().
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10043
    
    6

A .java file can have ONE public class, but may have none.

A public class may or may not have a main() method.

also, this is not true:
it should be compiled as : java PracticeTest

that is how you RUN it, not compile it. to compile it, you would have done something like

javac PracticeTest.java


Never ascribe to malice that which can be adequately explained by stupidity.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: getting a java.lang.NoSuchMethodError: main
 
Similar Threads
Wrapper classes doubt?
[floats and doubles] float f = 80/9; // f=8.0!!
mock01a Q36
"Non-static method cannot be referenced from a static context" ?
one question about casting