A place for Extreme Java JEE developers to read latest Java tutorials. The Extreme Java blog is for Java application development, J2EE application development and web technologies in the Java world.
Apr 3, 2012
How to read command line parameters in main method Java
1) The main method in Java has the signature as public static void main(String[] args)
2) The argument provided to the main method is a string array with elements as the strings passed to it when invoking the Java program.
3) The class name and the keyword java are not counted in this String array.
4) In eclipse, one can use run configuration option and provide any vm arguments which will get collected in String array named args.
5) The following code shows how to use command line arguments with main method in Java:
public class Test{
public static void main(String[] args) {
System.out.println(args[0]);
}
}
The way to run the above program with command line arguments is:
java Test Hi
The output with above invocation will be "Hi"
6) It is worth mentioning here that if no command line arguments are passed then accessing the String array arguments will result in ArrayIndexOutOfBoundsException
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment