Command Line Arguments in Java Last Updated : 10 Aug, 2025 Suggest changes Share 178 Likes Like Report Java command-line argument is an argument, i.e., passed at the time of running the Java program. Command-line arguments passed from the console can be received by the Java program and used as input. Example:java Geeks Hello WorldNote: Here, the words Hello and World are the command-line arguments. JVM will collect these words and will pass these arguments to the main method as an array of strings called args. The JVM passes these arguments to the program inside args[0] and args[1].Example: In this example, we are going to print a simple argument in the command line. Java // Java Program to Illustrate First Argument class GFG{ public static void main(String[] args) { // Printing the first argument System.out.println(args[0]); } } Output: Output of first argumentExplanation: Running java GFG GeeksForGeeks prints GeeksForGeeks because the argument is passed to main(String[] args). If no arguments are given (e.g., java GFG), it throws ArrayIndexOutOfBoundsException since args is empty.Why Use Command Line Arguments?It is used because it allows us to provide input at runtime without modifying the whole program.It helps to run programs automatically by giving them the needed information from outside.Working of Command-Line ArgumentsCommand-line arguments in Java are space-separated values passed to the main(String[] args) method. JVM wraps them into the args[] array, where each value is stored as a string (e.g., args[0], args[1], etc.). The number of arguments can be checked using args.length.Example: Display Command-Line Arguments Passed to a Java ProgramTo compile and run a Java program in the command prompt, follow the steps written below.Save the program as Hello.javaOpen the command prompt window and compile the program- javac Hello.javaAfter a successful compilation of the program, run the following command by writing the arguments- java HelloFor example - java Hello Geeks at GeeksforGeeksPress Enter and you will get the desired output. Java class Geeks { // Main driver method public static void main(String[] args) { // Checking if length of args array is // greater than 0 if (args.length > 0) { // Print statements System.out.println("The command line" + " arguments are:"); // Iterating the args array // using for each loop for (String val : args) System.out.println(val); } else System.out.println("No command line " + "arguments found."); } } Output: T twinkle tyagi 178 Article Tags : Java java-basics Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like