aspose file tools*
The moose likes Beginning Java and the fly likes String[] args Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String[] args" Watch "String[] args" New topic
Author

String[] args

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
Can anyone tell me why the main method in java cannot be compiled without the argument String[] args
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10372
    
    8

Why do you think it can't be compiled? The following code does compile.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
My question is why does the main method needs an argument ?? why can't it be compiled without an argument ??
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4388
    
    8

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

Joined: Jul 26, 2007
Posts: 10372
    
    8

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

Joined: Oct 02, 2003
Posts: 11312
    
  16

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:


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
fred i compiled and checked main method without any parameter is not compiling. JVM throws an error.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4388
    
    8

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

Joined: Oct 02, 2003
Posts: 11312
    
  16

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

Joined: Jun 19, 2008
Posts: 147


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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

And what command did you type at the command line to cause that error to appear?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4388
    
    8

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

Joined: Oct 02, 2003
Posts: 11312
    
  16

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

Joined: Jun 19, 2008
Posts: 147
thanks everyone for your concern
pen lin
Greenhorn

Joined: Aug 30, 2011
Posts: 1
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

Joined: Jan 28, 2007
Posts: 91
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,

Jitendra
SCJP1.5
SCWCD1.5
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10372
    
    8

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

Joined: Apr 06, 2010
Posts: 4388
    
    8

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

Joined: Jan 28, 2007
Posts: 91
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,

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String[] args