Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Error running Correctly compiled code  RSS feed

 
I. Haq
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading Oracle Certified Associate Java SE 8 Programmer 1 Study Guide Exam Book and there I read that all the followings are correct:

Even though the manual explicitly says that all these methods should run error free but this is not the case.
Where I'm wrong in this?

Best Regards,

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The signature is incorrect. The java program expects a main() method that takes a string array (for the command line arguments).

Henry
 
I. Haq
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then why did the code compiled in the first plaace?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cyber Man wrote:Then why did the code compiled in the first plaace?


There is nothing wrong with having a main() method that takes a string -- as it can be called by other parts of your program. However, if you want that main() method to be the starting main() method of the java application, the signature is not correct.

Henry
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I. Haq wrote:Then why did the code compiled in the first plaace?
  • Because main method in your example is an ordinary method you can have as many as such methods in your program but when you run your program using java command then JVM looks for a special main method having following signature declared with public static modifiers in the class whose name you mention whilst running the program.

  • Example:
  • This special main method is an entry point of the program. Above program compiles successfully, when you run above program using command java Test here JVM searches for special main method and executes It where It prints "I'm special main method" then invokes other main method having String as parameter which prints "Hi I'm an ordinary main method"


  • If you don't have such special main method then program can be compiled successfully ( If syntactically everythng is correct ) but when you run using java command then It will give runtime exception saying
    Error: Main method not found in class Test, please define the main method as:   public static void main(String[] args)
  •                                   
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!