• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Error running Correctly compiled code

 
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,

 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Saloon Keeper
Posts: 1217
85
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • 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)
  •                                  
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!