Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

getting a java.lang.NoSuchMethodError: main

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i just wrote my first program in Java and got that error.
please help , what should i do.


 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you trying to run this code? Class PracticeTest should be public, and then you'd enter "java PracticeTest" on the command line.
 
Marshal
Posts: 76113
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Have you got your problem sorted yet?
 
yadav mayank
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the class that has the main method has to be accessible from the outside.
 
yadav mayank
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ().
 
lowercase baba
Posts: 13063
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic