Command Line arguments in Java

Course Curriculum

Command Line arguments in Java

Command Line arguments in Java

If we run a Java Program by writing the command “java Hello learn programming at prutor.ai” where the name of the class is “Hello”, then it will run upto Hello. It is command upto “Hello” and after that i.e “Prutor At PrutordotAi”, these are command line arguments.

When command line arguments are supplied to JVM, JVM wraps these and supply to args[]. It can be confirmed that they are actually wrapped up in args array by checking the length of args using args.length

class HelloCmd
{
public static void main(String[] args)
{
// check if length of args array is
// greater than 0
if (args.length > 0)
{
System.out.println("The command line"+
" arguments are:");
// iterating the args array and printing
// the command line arguments
for (String val:args)
System.out.println(val);
}
else
System.out.println("No command line "+
"arguments found.");
}
}

Output :

DoubleStream mapToObj() in Java (Prev Lesson)
(Next Lesson) Scanner Class in Java