aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes main without string args[ ] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "main without string args[ ]" Watch "main without string args[ ]" New topic
Author

main without string args[ ]

srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
can anyboyd pls explain this,i got a class with main() without string args,it compiles but doesnt run.,i know why main is declared as static and public,but what is exactly string args[] doing in main() method?
public class mainwithoutargs
{
public static void main()
{
System.out.println("im in main w/o string args[]");
}
}


please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
the string args in main method is the string array which holds any arguments invoked on the command line. Java requires you to have a main with signature(string[]) so as to run that application. For example
try this program

and invoke it java mainwithargs one two
see the results.
Originally posted by srinivas bolloju:
can anyboyd pls explain this,i got a class with main() without string args,it compiles but doesnt run.,i know why main is declared as static and public,but what is exactly string args[] doing in main() method?
public class mainwithoutargs
{
public static void main()
{
System.out.println("im in main w/o string args[]");
}
}


[This message has been edited by Anshul Manisha (edited July 06, 2001).]


AM<BR> <A HREF="mailto:anshulmohan@rediffmail.com" rel="nofollow">anshulmohan@rediffmail.com</A>
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
hi anshul,in the class which u posted, u are expecting i.e the class is expecting some arguments to be passed, so args[0] prints one,
in my class ,im not expecting any args to be used inside the program.,so for me there is no use of args[].,that is ....i will run the program w/o any args....like java mainwithoutargs that's it,.......
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Hi srinivas,
In every main method of any Java program you need to put an String Array as argument.
But in your program you does not need to use it.
You can have a main like
public static void main(String[] args)
and may invoke it with
java myClass
Then you have args.length=0 ::: no problem.
For me it is a suprise that main method like
public static void main()
even compiles?!!
Hope this helps.
Axel
[This message has been edited by Axel Janssen (edited July 06, 2001).]
krishna avrs
Greenhorn

Joined: May 23, 2001
Posts: 4
Hi,
The above code works because main is like any other method. We can overload that method. But Java Run Time will look for
public static void main(String args[]) for running the application.
Try the below code.
public class mainwithoutargs
{
//overloaded method.
public static void main()
{
System.out.println("im in your main w/o string args[]");
}
public static void main(String args[])
{
System.out.println("im in my main w/o string args[]");
//calling overloaded method.
main();
}
}
Cheers!
SriKi.
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
For me it is a suprise that main method like
public static void main()
even compiles?!!

Axel,
Its nothing to feel surprise. The compiler doesn't complain with the no argument main() method because it thinks that its just an ordinary method.


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Note that failure to include a method with the signature public static void main(String[] args) , or its equivalent, does not cause a compile-time error, but you will get a runtime exception because the "main" thread can't find the method it needs to run.
[This message has been edited by Scott Appleton (edited July 06, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If you don't supply any parameters then args is simply an empty array.
------------------
Co-Moderator of the Programmer Certification Forums


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Hassan Naqvi
Ranch Hand

Joined: May 03, 2001
Posts: 158
Hi,
I think String args[] is compulsary part of main() signature.In many cases we have no need of String array.But it's an essential part which all main() methods have.
Regards,
Hassan.


Always Belive On Logic!!
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
until now it has been a discussion like ...java needs
String args[] in the main() for successful execution,
but my question is why does it need that....??
but my question is why does it need that....??
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

but my question is why does it need that....??
but my question is why does it need that....??

Because that is the method signature that the Java VM looks for when it tries to run your program. That is the contract that you must adhere to if you want the JVM to run your program from the command line. When you type the command
java MyClass
the program you are actually running is the Java Virtual Machine (on DOS/Windows systems, it is the java.exe application). The JVM will then attempt to open and read in a file specified by the first command line parameter which, in the example above, is "MyClass". If it is successful in loading the class file, it will do some initialization and then attempt to invoke a static method main() that takes a single parameter: a String array. If the JVM finds that method signature in the class, it will take the rest of the command line parameters, if there are any, and put them in a String array. If there are no command line parameters other than the class name, then the JVM will have an empty String array. At any rate, that String array is what the JVM passes to the main() static method. That's all it needs to do and if it can't do it because it can't find a static main() method that takes a String array, then it will generate an exception.


Junilu - [How to Ask Questions] [How to Answer Questions]
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
i got it..thanx lacar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main without string args[ ]