See the Tutorial List

Command line argument in Java

The command line argument is the argument passed to a program at the time when you run it. To access the command-line argument inside a java program is quite easy, they are stored as string in String array passed to the args parameter of main() method.

Example

class cmd
{
  public static void main(String[] args)
  {
    for(int i=0;i< args.length;i++)
    {
    System.out.println(args[i]);
    }
  }
}

Execute this program as java cmd 10 20 30

10 20 30

command line argument


System.exit()

In Java, exit() method is in the class java.lang.System.exit(). This method is used to take an exit or terminating from a running program. A non zero value is set in a status code which is used for indicating a non-zero value. exit(0) is used for successful termination and exit(1) or exit(-1) is used for unsuccessful termination. The exit() method does not return any value.

Example:

	
import java.util.*; 
import java.lang.*; 

class ExitDemo1
{ 
    public static void main(String[] args) 
    { 
intx[] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}; 

        for (inti = 0; i<x.length; i++) 
        { 
            if (x[i] >= 40) 
            { 
System.out.println("Program is Terminated..."); 
System.exit(0); 
            } 
            else
System.out.println("x["+i+"] = " + x[i]); 
        } 
    } 
}
	

system-exit-program