{"id":1402,"date":"2016-06-07T16:47:10","date_gmt":"2016-06-07T16:47:10","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1402"},"modified":"2022-10-03T06:41:01","modified_gmt":"2022-10-03T06:41:01","slug":"command-line-arguments-to-a-java-program","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/command-line-arguments-to-a-java-program\/","title":{"rendered":"Java Program to Print Command line arguments"},"content":{"rendered":"<h1>Overview<\/h1>\n<p>In this tutorial, we will write a simple Java program to print the command line argument passed to the Java program.<\/p>\n<p>In Java, we have a special kind of array called <em>args<\/em> that holds the command line arguments passed to the Java program. args[0] holds the first command-line argument. args[1] holds the second command-line argument and so on.<\/p>\n<h2><strong>Simple Java Program<br \/>\n<\/strong><\/h2>\n<p>An array stores a series of values. The values can be numbers, strings, objects, or just any other kind of Java data type. In Java, the first element of the array is at position 0, the second at position 1, and so on. This is illustrated in the below diagram.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1167\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/CommandLine-Arguments-in-Java.png\" alt=\"Command Line Arguments in Java\" width=\"1131\" height=\"607\" title=\"\"><\/p>\n<p>To understand this better, Let\u2019s see with the help of a simple example program called <em>Greeting<\/em>. You can pass the person&#8217;s name as a command line argument to the java program.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class Greeting {\r\n public static void main (String args[]) {\r\n \/* Say hello to person passed in command line *\/\r\n System.out.println(\"Hello \" + args[0] );\r\n }\r\n }<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>Program Output<br \/>\n<\/strong><\/h2>\n<p>Compile the Java program<\/p>\n<p><em>\/&gt; javac Greeting.java<\/em><\/p>\n<p>Run the program by passing the name as an argument to the program.<\/p>\n<p><em>\/&gt; java Greeting Surendra<\/em><\/p>\n<p>The first argument is stored in args[0]. The program picks it up and displays the output as shown below.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1169\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/CommandLine-Arguments-Java-2.png\" alt=\"Command line args\" width=\"1341\" height=\"685\" title=\"\"><\/p>\n<h2><strong>Exercise<\/strong><\/h2>\n<p>As an exercise, you can improvise the above code snippet<\/p>\n<p>Task1: To avoid the common exception, handle the exception using a <em>try-catch<\/em> block.\u00a0 <strong>ArrayIndexOutOfBoundsException<\/strong> while dealing with the command line.<\/p>\n<p>Task2: This program only prints one command line argument. Using a loop, enhance the program to greet all the names provided in the command line.<\/p>\n<p>&#8212;<\/p>\n<p>Java Tutorial on this website: <strong><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/strong><\/p>\n<p>For more information on Java, visit the official website :<\/p>\n<p><strong><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/strong><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1402\" data-postid=\"1402\" class=\"themify_builder_content themify_builder_content-1402 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Overview In this tutorial, we will write a simple Java program to print the command line argument passed to the Java program. In Java, we have a special kind of array called args that holds the command line arguments passed to the Java program. args[0] holds the first command-line argument. args[1] holds the second command-line [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-1402","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1402"}],"version-history":[{"count":13,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1402\/revisions"}],"predecessor-version":[{"id":21033,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1402\/revisions\/21033"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}