{"id":77656,"date":"2020-04-09T11:00:31","date_gmt":"2020-04-09T05:30:31","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=77656"},"modified":"2020-04-09T11:00:31","modified_gmt":"2020-04-09T05:30:31","slug":"java-command-line-arguments","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/","title":{"rendered":"Java Command Line Arguments with Examples"},"content":{"rendered":"<p>We know that Java is a versatile programming language. It is a platform-independent programming language. The execution of a Java program is less complex, quite easy, and error-free. There are many situations that we face during programming where we need to pass the input while executing the program.<\/p>\n<p>In our previous article, we studied how to take input from the console, in this article, we will learn <strong>how a user can pass java command-line arguments during the execution of a program.<\/strong><\/p>\n<h3>What Are Java Command Line Arguments?<\/h3>\n<p>The command-line arguments in Java allow the programmers to pass the arguments during the execution of a program. The users can pass the arguments during the execution by passing the command-line arguments inside the main() method.<\/p>\n<p>A command-line argument is nothing but the information that we pass after typing the name of the Java program during the program execution. These arguments get stored as Strings in a String array that is passed to the main() function.<\/p>\n<p>We can use these command-line arguments as input in our Java program.<\/p>\n<h4>Java Command-line arguments Example with its working<\/h4>\n<p>Suppose there is a java program and we compile it. During the execution of the program, we pass the command as \u201cjava MyProgram Techvidvan Java Tutorial\u201d. Here the java command is used to run the Java program in which the main function is present and,<\/p>\n<p>MyProgram is the name of the java file that we need to run. JVM considers the input after the program name as command-line arguments. That is, in our input command, \u201cTechvidvan Java Tutorial\u201d is considered as command-line arguments in the form of a String array.<\/p>\n<p>We need to pass the arguments as space-separated values. We can pass both strings and primitive data types(int, double, float, char, etc) as command-line arguments. These arguments convert into a string array and provided to the main() function as a string array argument.<\/p>\n<p>Internally, JVM wraps up these command-line arguments into the args[ ] array that we pass into the main() function. We can check these arguments using args.length method.<\/p>\n<p>JVM stores the first command-line argument at args[0], the second at args[1], third at args[2], and so on.<\/p>\n<p>Let\u2019s learn how to use command-line arguments in our Java program.<\/p>\n<h5>Java code to understand the command-line arguments<\/h5>\n<p>To understand the command-line argument we will create a file named CommandLine.java and write the following code to display all the arguments that we will pass through the command-line:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/Display all command-line arguments.\nclass CommandLine\n{\n    public static void main(String args[ ])\n    {\n        System.out.println(\u201cThe command-line arguments are:\\n\u201d);\n        for (int x = 0; x &lt; args.length; x++)\n            System.out.println(\"args[\" + x + \"]: \" + args[ x ]);\n    }\n}\n<\/pre>\n<p><strong>Steps to run the above program<\/strong><\/p>\n<p>To compile and run a java program in command prompt, follow the steps written below.<\/p>\n<ul>\n<li>Save the program as CommandLine.java<\/li>\n<li>Open the command prompt window and compile the program- javac CommandLine.java<\/li>\n<li>After a successful compilation of the program, run the following command by writing the arguments- java CommandLine argument-list<\/li>\n<li>For example &#8211; java CommandLine TechVidvan Java Tutorial<\/li>\n<li>Press Enter and you will get the desired output.<\/li>\n<\/ul>\n<p>After performing the above steps, we will get the following output:<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-15-53-36.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78154\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-15-53-36.png\" alt=\"Command line in java example\" width=\"1533\" height=\"873\" \/><\/a><\/p>\n<h4>Example of command-line argument in java that prints only one argument<\/h4>\n<p>We can also display a single argument in the command prompt. Following code shows this example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/Display one command-line argument.\nclass CommandLine\n{\n    public static void main(String args[ ])\n    {\n        String name = args[3];\n        System.out.println(\"The name of the user is: \" +name);\n    }\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-15-48-24.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78155\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-15-48-24.png\" alt=\"Command line argument in java with example\" width=\"1533\" height=\"873\" \/><\/a><\/p>\n<h4>How to Pass Numeric Command-Line Arguments in Java?<\/h4>\n<p>The main() method of a Java program only accepts the data with the string type arguments. Therefore, we can never pass numeric arguments through the command line.<\/p>\n<p>If we pass them, they are converted int string and we can\u2019t use them as numbers and can\u2019t perform any numeric operations with them.<\/p>\n<p>But, we can convert these string(numeric) arguments into numeric values. Let\u2019s see an example to understand it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class NumericArguments\n{\n    public static void main(String[] args)\n    {\n        \/\/ convert into integer type\n        int number1 = Integer.parseInt(args[0]);\n        System.out.println(\"First Number: \" +number1);\n\n        int number2 = Integer.parseInt(args[1]);\n        System.out.println(\"Second Number: \" +number2);\n        int result = number1 + number2;\n        System.out.println(\"Addition of two numbers is: \" +result);\n    }\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-16-14-47.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78156\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-16-14-47.png\" alt=\"Passing Numeric Command-Line Arguments in java\" width=\"1533\" height=\"873\" \/><\/a><\/p>\n<p>In the above program, we used the parseInt() method of the Integer class to convert the string argument into an integer.<\/p>\n<p>Similarly, we can use the parseDouble() to convert the string into a double value and parseFloat() method to convert the string into a float value.<\/p>\n<h4>How to Pass Command Line Arguments in Eclipse IDE<\/h4>\n<p>We can also pass command-line arguments in Eclipse IDE using Run Configurations.<\/p>\n<h4>Step 1<\/h4>\n<p>Open the Java program in Eclipse<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-16-46-05.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78157\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-16-46-05.png\" alt=\"Open java program in eclipse\" width=\"1533\" height=\"821\" \/><\/a><\/p>\n<h4>Step 2<\/h4>\n<p>From the editor, right-click and choose &#8220;Run As&#8221; option. Inside it, select the &#8220;Run Configurations\u2026 option&#8221;. After choosing Run Configurations, you will see following screen:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-16-49-50.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78158\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-16-49-50.png\" alt=\"Run configuration in java\" width=\"1533\" height=\"821\" \/><\/a><\/p>\n<h4>Step 3<\/h4>\n<p>In the pop-up window, click on the Arguments tab. Then provide the command line argument value in the &#8220;Program Arguments&#8221; text box.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-16-51-18.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78159\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-16-51-18.png\" alt=\"Java arguments tab\" width=\"1533\" height=\"821\" \/><\/a><\/p>\n<h4>Step 4<\/h4>\n<p>Click on the Run button<\/p>\n<p>When you will click on the Run button, you will get the following output:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-16-51-24.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78160\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-16-51-24.png\" alt=\"Command line arguments output\" width=\"1533\" height=\"821\" \/><\/a><\/p>\n<h4>Calculating Factorial of a number through the command-line<\/h4>\n<p>Now, after having the knowledge about the command-line arguments and their usage, we can start using it in various calculations. Let\u2019s try to calculate the factorial of a number in which we pass the number(whose factorial we need to calculate) as the command-line argument:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class Factorial\n{\n    public static void main(String[] args)\n    {\n        int i , fact = 1;\n        int number = Integer.parseInt(args[0]);\n        for(i = 1; i&lt;= number ; i++)\n        {\n            fact = fact * i;\n        }\n        System.out.println(\"The factorial of \" + number + \" is \" +fact);\n    }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-17-02-00.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78161\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/Screenshot-from-2020-03-11-17-02-00.png\" alt=\"Java factorial example\" width=\"1533\" height=\"873\" \/><\/a><\/p>\n<h4>Important Points To Remember About Command-line Arguments<\/h4>\n<ul>\n<li>We can use the command-line arguments to specify the configuration information while launching our Java application.<\/li>\n<li>We can use as many arguments in the command line as we need to. There is no limitation on the number of command-line arguments.<\/li>\n<li>The data in command-line arguments are in the form of String.<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>This brings us to the end of the article on Java Command line arguments. We learned that Command line arguments in Java are passed at the execution of the program and the JVM provides them to the args string in the main() method of the Java program.<\/p>\n<p>We came to know how can we pass the String as well as the numeric arguments to the command line as the user input. You can also run the command line arguments in the IDE such as Eclipse whose step by step process id discussed in this article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We know that Java is a versatile programming language. It is a platform-independent programming language. The execution of a Java program is less complex, quite easy, and error-free. There are many situations that we&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":78163,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[2333,2334,2335,2336,2337,2338],"class_list":["post-77656","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-command-argument-in-java","tag-command-line-arguments-in-java","tag-java-command-argument","tag-java-command-argument-example","tag-java-command-line-arguments","tag-java-command-line-arguments-example"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Command Line Arguments with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"What is java command line arguments? Learn with easy examples and programs. Learn points to remember while using command line arguments to avoid errors.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Command Line Arguments with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"What is java command line arguments? Learn with easy examples and programs. Learn points to remember while using command line arguments to avoid errors.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-09T05:30:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/command-line-arguments-in-java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Command Line Arguments with Examples - TechVidvan","description":"What is java command line arguments? Learn with easy examples and programs. Learn points to remember while using command line arguments to avoid errors.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/","og_locale":"en_US","og_type":"article","og_title":"Java Command Line Arguments with Examples - TechVidvan","og_description":"What is java command line arguments? Learn with easy examples and programs. Learn points to remember while using command line arguments to avoid errors.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-04-09T05:30:31+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/command-line-arguments-in-java.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Java Command Line Arguments with Examples","datePublished":"2020-04-09T05:30:31+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/"},"wordCount":963,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/command-line-arguments-in-java.jpg","keywords":["Command Argument in Java","Command line arguments in Java","Java Command Argument","Java Command Argument Example","Java Command Line Arguments","Java command line arguments example"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/","url":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/","name":"Java Command Line Arguments with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/command-line-arguments-in-java.jpg","datePublished":"2020-04-09T05:30:31+00:00","description":"What is java command line arguments? Learn with easy examples and programs. Learn points to remember while using command line arguments to avoid errors.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/command-line-arguments-in-java.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/command-line-arguments-in-java.jpg","width":802,"height":420,"caption":"Java Command Line arguments"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-command-line-arguments\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java Command Line Arguments with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=77656"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77656\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/78163"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=77656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=77656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=77656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}