- Command-line arguments
There is a main method in all Java programs, and this method takes a parameter, string args[]. This parameter is the parameter list of the user input that the main method accepts, which is the command-line argument.
- An example of the value of the direct output command line parameter
1 Public classArgsdemo {2 Public Static voidMain (String[]args) {3 4 intNumber=args.length;5System.out.println ("A total of +number+" parameters ");6 for(inti=0;i<args.length;i++)7System.out.println ("+i+" parameter: "+args[i]); 8 } 9}
Windows compilation
1. Save As Argsdemo.java file (note that the file name is the same as the public class name). If saved to E:\jobset\ArgsDemo.java
2.ctrl+r open cmd. (1) Order e: Enter the e-disk; (2) command cd:\jobset into the directory where the Java files are located;
3. Compile and execute Java files in the Java file directory. (1) Command: Javac Argsdemo.java (2) command: Java argsdemo My name is Marry (where my name is Marry is the input parameter);
Linux Run jar
1. You can use Eclipse to export the program to a jar file and then put it into the \home\yonghuming\argsdemo.jar
2. Terminal execution command: Java-jar \home\yonghuming\argsdemo.jar My name is Marry
- Example 2--to upload command line arguments to list
1 Importjava.util.ArrayList;2 Importjava.util.List;3Since the first, second, and last word will be taken, please enter at least three words.4 Public classArgslist {5 Public Static voidMain (String[]args) {6 7List<string> list=NewArraylist<string>();8 9 for(String Temp:args)Ten List.add (temp); One ASystem.out.println ("Enter list complete"); - for(String temp:list) { -System.out.print (temp+ "\ T"); the } -System.out.println ("\ n"); - System.out.println (List.size ()); - +System.out.println (list.get (0) + "\ n" +list.get (1) + "\ n" +list.get (List.size ()-1)); - + } A}
After the command-line arguments are entered into the args[] array, they can be used just like normal arrays.
The compile execution method is the same as above.
Java command line parameter input and command line parameter input data into list