Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String[] args

 
shivang sarawagi
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me why the main method in java cannot be compiled without the argument String[] args
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think it can't be compiled? The following code does compile.
 
shivang sarawagi
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is why does the main method needs an argument ?? why can't it be compiled without an argument ??
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can be compiled without an argument - Maneesh's example just proves it.

But it won't be recognised by the JVM as a starting point for a program. And that's simply because the JVM is looking for a method public static void main(String[]). Nothing else will do - that's just how the JVM is written.

As to why - the argument is there so that command-line arguments can be passed into the program.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure I understand what you are saying here. Arguments to the main method are passed at runtime, not compile time.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even more interestingly, this will also compile:


Maneesh's example DOES have a main method with a String [] argument - but you really don't need that at all. You don't need any argument. This also compiles:

 
shivang sarawagi
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred i compiled and checked main method without any parameter is not compiling. JVM throws an error.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error message are you getting? And what does your code look like? Because the real reason it's not compiling is probably something else.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivang sarawagi wrote:fred i compiled and checked main method without any parameter is not compiling. JVM throws an error.

please read our FAQ on why ItDoesntWorkIsUseless. then post both your code and the exact and complete text of the error you are getting, and lots of folk will be thrilled to help you.

Without that information, all we can do is make WAGs as to what your problem is.
 
shivang sarawagi
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is my code. The error i am getting is :"Error: Main method not found in class Flex, please define the main method as:
public static void main(String[] args)"
 
Paul Clapham
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what command did you type at the command line to cause that error to appear?
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that's not a compiler error. That's a run-time error - you must be doing something that is attempting to run the class, rather than just compiling it. In which case, the error is because of the reasons previously explained.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you can see, it COMPILES just fine:

C:\slop>type Flex.java

C:\slop>javac Flex.java
C:\slop>


However, you can't RUN it:


C:\slop>java Flex
Exception in thread "main" java.lang.NoSuchMethodError: main

C:\slop>

That's because when you try and run a class, the JVM explicitly looks for a method with the signiature of "public static void main (String [] args)".
 
shivang sarawagi
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everyone for your concern
 
pen lin
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Methods have their own method signature.
And method signature = method name + method argument types.
JVM to run the main method as a entry point.
If you remove the type parameter so that JVM to can not find main method.
So you can compiled it without any errors ,but you can't run it.
P.S.My English is poor,if you find any grammar mistakes, please tell me to correct it。
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the poster here wants to know the rationale behind the requirement of main() with String array argument.

The question she should ask is,

Why did they(creators of java) made it compulsory for main to have an argument.....
Since it is not a requirement in c#.....
What advantage does it(making it compulsory) give us.


Even if she did not ask it,this question has perflexed me too, so kindly help me understand it.

Thanks and regards,
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jitendra Jha wrote:
Why did they(creators of java) made it compulsory for main to have an argument.....
What advantage does it(making it compulsory) give us.
Even if she did not ask it,this question has perflexed me too, so kindly help me understand it.

Fred has already answered it above
Fred wrote:That's because when you try and run a class, the JVM explicitly looks for a method with the signiature of "public static void main (String [] args)".


 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Fred has already answered it above

Well, Jitendra's right that they could have designed it in a different way.

I think the answer is probably that it's simpler, because:

- There's only one valid way of declaring the main method.
- If more than one way of writing main() is valid, what happens if you overload it? The JVM wouldn't know which to run. OK, it could run one if command-line arguments are given, and the other otherwise, but that's just going to confuse people.
- If you wrote a main() with no arguments, and then tried to pass in command-line arguments it wouldn't work. This will also confuse people.

So keep it simple - having to add a String[] even if you don't use it is hardly difficult.

[Edited to fix the override/overload "deliberate mistake" ]
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew for that reasoning.

We of course cannot be sure as to what exactly was their reason but the way you have put it is probably the right way to think about it.

Regards,

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic