{"id":1984,"date":"2011-04-14T12:15:27","date_gmt":"2011-04-14T06:45:27","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1984"},"modified":"2011-04-14T12:15:27","modified_gmt":"2011-04-14T06:45:27","slug":"compile-and-run-java-program-in-package-from-command-line","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/compile-and-run-java-program-in-package-from-command-line\/","title":{"rendered":"Compile and run Java program in package from command line"},"content":{"rendered":"<p>The topic is very easy and i am sure that lots of java programmer already know how to compile java program existing in package. However there are users who frequently works on eclipse, netbeans or any other IDE and don&#8217;t know that how the program actually works behind the IDE. So this article basically emphasizes on basics of Java Compilation.<\/p>\n<p>Consider following directory structure :<\/p>\n<figure id=\"attachment_1985\" aria-describedby=\"caption-attachment-1985\" style=\"width: 154px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Java-Directory-structure.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1985\" title=\"Java Directory structure\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Java-Directory-structure.jpg?resize=154%2C106&#038;ssl=1\" alt=\"Java Directory structure\" width=\"154\" height=\"106\" \/><\/a><figcaption id=\"caption-attachment-1985\" class=\"wp-caption-text\">Java Directory structure<\/figcaption><\/figure>\n<p><!--more-->Our java files will be in &#8220;<span style=\"text-decoration: underline;\">src<\/span>&#8221; folder. I want all the class files in folder &#8220;<span style=\"text-decoration: underline;\">classes<\/span>&#8221; and currently i am in folder &#8220;<span style=\"text-decoration: underline;\">Exec Jar<\/span>&#8220;.<\/p>\n<p>Consider below Java Files:<\/p>\n<p>Person.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.g2.ExecJar;\n\npublic class Person {\n\tprivate String name;\n\tprivate int age;\n\n\tpublic String getName() {\n\t\treturn name;\n\t}\n\n\tpublic void setName(String name) {\n\t\tthis.name = name;\n\t}\n\n\tpublic int getAge() {\n\t\treturn age;\n\t}\n\n\tpublic void setAge(int age) {\n\t\tthis.age = age;\n\t}\n}\n\n<\/pre>\n<p>Start.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.g2.ExecJar;\n\nimport java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\n\npublic class Start {\n\n\tpublic static void main(String&#x5B;] args) {\n\n\t\ttry {\n\t\t\tInputStreamReader io = new InputStreamReader( System.in );\n\t\t\tBufferedReader read = new BufferedReader(io);\n\n\t\t\tSystem.out.println(&quot;Enter Name&quot;);\n\t\t\tPerson p = new Person();\n\t\t\tp.setName(read.readLine());\n\n\t\t\tSystem.out.println(&quot;Enter Age&quot;);\n\t\t\tp.setAge(Integer.parseInt( read.readLine()) );\n\n\t\t\tSystem.out.println( &quot;-------- You have Eneterd --------- &quot; );\n\t\t\tSystem.out.println(&quot;Name : &quot; + p.getName());\n\t\t\tSystem.out.println(&quot;Age : &quot; + p.getAge());\n\n\t\t\tSystem.out.println( &quot;---------- Progarm run by executable jar ------------ &quot; );\n\n\t\t} catch (IOException e) {\n\t\t\te.printStackTrace();\n\t\t} catch (NumberFormatException e) {\n\t\t\tSystem.out.println(&quot;Age entered is not in correct format&quot;);\n\t\t}\n\t}\n}\n\n<\/pre>\n<p>Run below steps to compile and run java programs from command line:<\/p>\n<figure id=\"attachment_1987\" aria-describedby=\"caption-attachment-1987\" style=\"width: 481px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Compile-and-run-Java-Program-in-Package-from-Command-Line.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1987  \" title=\"Compile and run Java Program in Package from Command Line\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Compile-and-run-Java-Program-in-Package-from-Command-Line.jpg?resize=481%2C152&#038;ssl=1\" alt=\"Compile and run Java Program in Package from Command Line\" width=\"481\" height=\"152\" \/><\/a><figcaption id=\"caption-attachment-1987\" class=\"wp-caption-text\">Compile and run Java Program in Package from Command Line<\/figcaption><\/figure>\n<p><span style=\"text-decoration: underline;\"><strong>Command Explanation :<\/strong><\/span><\/p>\n<p><strong>Line 1 : <\/strong><\/p>\n<ul>\n<li>&#8220;javac&#8221; is the java compiler available in bin folder of the jdk.<\/li>\n<li>&#8220;<strong>-d<\/strong>&#8221; stands for the &#8220;<strong>directory<\/strong>&#8220;. it explains compiler that where the class files should be created.<\/li>\n<li>Last argument is the Complete path, where the java file exists.<\/li>\n<\/ul>\n<p><strong>Line 2 :<\/strong><\/p>\n<ul>\n<li>in line 2, you have noted one extra parameter &#8220;<strong>-classpath<\/strong>&#8220;. As class &#8220;Start&#8221; depends on class &#8220;Person&#8221; and its class file is not in the same directory.\u00a0 Therefore we need to explicitly tell compiler that where it can find required class files.<\/li>\n<li>To include more than one classpath use semicolon &#8220;;&#8221;. \u00a0Example: \u00a0<strong>-classpath path1;path2;path3<\/strong><\/li>\n<\/ul>\n<p><strong>Line 3 :<\/strong><\/p>\n<ul>\n<li>It will run the program. her we have to again specify that where all the class files exist with the help of parameter &#8220;<strong>-classpath<\/strong>&#8220;<\/li>\n<\/ul>\n<p><a title=\"javac supported arguments\" href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/tooldocs\/solaris\/javac.html\" target=\"_blank\">The complete set of argument supported by the java compiler can be found on the Oracle site.<\/a><\/p>\n<p>Next article, How to<a title=\"Executable jar file\" href=\"https:\/\/jitendrazaa.com\/blog\/java\/create-executable-jar-file-of-classes-in-package\/\" target=\"_blank\"> create executable jar<\/a> file for packages in java<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Compile JAVA Program in Package from command line <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3],"tags":[329],"class_list":["post-1984","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1993,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/create-executable-jar-file-of-classes-in-package\/","url_meta":{"origin":1984,"position":0},"title":"Create Executable jar file of classes in package","author":"Jitendra","date":"April 14, 2011","format":false,"excerpt":"Create Executable jar file of classes in package","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2347,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/hibernate\/step-by-step-hibernate-tutorial-using-eclipse-wtp\/","url_meta":{"origin":1984,"position":1},"title":"Step By Step Hibernate Tutorial Using eclipse WTP","author":"Jitendra","date":"August 8, 2011","format":false,"excerpt":"Step By Step Hibernate (ORM Tool) Tutorial Using eclipse WTP","rel":"","context":"In &quot;Hibernate&quot;","block_context":{"text":"Hibernate","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/hibernate\/"},"img":{"alt_text":"Eclipse Install New Software - Hibernate","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Eclipse-Install-New-Software-Hibernate.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3213,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/resolve-error-artifacttransferexception-could-not-transfer-artifact-or-failure-to-transfer-in-maven\/","url_meta":{"origin":1984,"position":2},"title":"Resolve Error &#8220;ArtifactTransferException: Could not transfer artifact&#8221; or &#8220;Failure to Transfer&#8221; in Maven","author":"Jitendra","date":"February 27, 2013","format":false,"excerpt":"This is very short tips to resolve the errors \"ArtifactTransferException: Could not transfer artifact\" that may occur in Maven project in Eclipse. Steps to resolve: Open folder by running this text (without Quotes) in Search Explorer of Window \"%USERPROFILE%.m2\". After running above command, \"m2\" folder of maven will open. Now\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1586,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/servlet\/read-file-present-inside-java-and-j2ee-project\/","url_meta":{"origin":1984,"position":3},"title":"Read File Present inside Java and J2EE Project","author":"Jitendra","date":"February 24, 2011","format":false,"excerpt":"How to Read File Present inside Java and J2EE Project","rel":"","context":"In &quot;Servlet&quot;","block_context":{"text":"Servlet","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/servlet\/"},"img":{"alt_text":"Read File Present inside Java and J2EE Project","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Read-File-Present-inside-Java-and-J2EE-Project.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":738,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/checked-unchecked-exception\/","url_meta":{"origin":1984,"position":4},"title":"Checked and Unchecked Exception","author":"Jitendra","date":"August 7, 2010","format":false,"excerpt":"Explains the concept of checked exception and unchecked exception in JAVA.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2892,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-migration-tool-ant\/","url_meta":{"origin":1984,"position":5},"title":"Complete Salesforce Deployment Guide using Ant Migration Tool","author":"Jitendra","date":"June 5, 2012","format":false,"excerpt":"Step by Step tutorial of Salesforce Migration using ANT tool with Proxy settings and retrieving content from Salesforce Organization. Also fix some common errors like java.lang.OutOfMemoryError or unable to find tools.jar","rel":"","context":"In &quot;Configuration&quot;","block_context":{"text":"Configuration","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/configuration\/"},"img":{"alt_text":"Salesforce Get ANT version ","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/06\/Salesforce-Get-ANT-version-Original.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/06\/Salesforce-Get-ANT-version-Original.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/06\/Salesforce-Get-ANT-version-Original.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1984"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1984\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}