Thursday, November 7, 2019

Using Command-Line Arguments in Java

Using Command-Line Arguments in Java Command-line arguments can be a way of specifying configuration properties for an application, and Java is no different. Instead of clicking on an application icon from the operating system, you can run the Java application  from a terminal window. Along with the application name, a number of arguments can follow which are then passed to the applications starting point (i.e., the main method, in the case of Java). For example, NetBeans has a number of startup parameters that can be passed to the application when it is run from a terminal window (e.g., specifies a version of the JDK to be used instead of the default JDK associated with the NetBeans application). The Main Method Lets examine the main method  to see where the arguments passed to an application appear: The command-line arguments can be found in the called For example, lets consider an application called whose only action is to print out the command-line arguments passed to it: public class CommandLineArgs {   Ã‚  Ã‚  public static void main(String[] args) {//check to see if the String array is emptyif (args.length 0){System.out.println(There were no commandline arguments passed!);}   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  //For each String in the String array//print out the String.for(String argument: args){System.out.println(argument);}}}   Syntax of Command Line Arguments The Java Runtime Engine (JRE) expects arguments to be passed following a particular syntax, like so: java ProgramName value1 value2 Above, java invokes the JRE, which is followed by the name of the program you are calling. These are followed by any arguments to the program. There is no limit to the number of arguments a program can take, but the order is critical. The JRE passes the arguments in the order in which they appear on the command line.  For example, consider this code snippet from above: public class CommandLineArgs2 {​   Ã‚  Ã‚  public static void main(String[] args) {if (args.length 0){System.out.println(There were no commandline arguments passed!);} When arguments are passed to a Java program, args[0] is the first element of the array (value1 above), args[1] is the second element (value2), and so on. The code args.length() defines the length of the array. Passing Command-Line Arguments In NetBeans, we can pass command-line arguments without having to build the application and run it from a terminal window. To specify the command-line arguments: Right-click on the project folder in the Projects window.Choose the Properties option to  open  Project Properties window.  In the Categories list on the right-hand side, choose RunIn the Arguments textbox that appears, specify the command-line arguments you want to pass to the application. For example, if we enter Apple Banana Carrot in the Arguments textbox and run the CommandLineArgs program listed above, we will get the output: Parsing the Command-Line Arguments Typically, a command line argument is passed with some information about what to do with the value being passed. The argument informing the application what the argument is for typically has a hyphen or two before its name. For example, the NetBeans example for the startup parameter specifying the JDK path is This means youll need to parse the command-line arguments to figure out what to do with the values. There are several Java command-line frameworks for parsing command-line arguments. Or you could write a simple command-line parser if the arguments you need to pass are not that many: The code above either prints the arguments or add them together if they are integers. For example, this command line argument would add the numbers: java CommandLineArgs -addnumbers 11 22 33 44

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.