{"id":71,"date":"2013-06-15T01:10:55","date_gmt":"2013-06-15T01:10:55","guid":{"rendered":"https:\/\/code4reference.com\/?p=71"},"modified":"2023-10-01T13:34:54","modified_gmt":"2023-10-01T13:34:54","slug":"code4referencehow-to-make-jar-file-using-jar-command-code4reference","status":"publish","type":"post","link":"https:\/\/code4reference.com\/?p=71","title":{"rendered":"How to make jar file using jar command."},"content":{"rendered":"<p>This is a simple tutorial about making jar file and compiling the source code using command. As you may know that in java the class are packaged in jar file and provides as library. So it can be used in different module\/project.<br \/>\nFirst we are going to make some suitable directory structure. Run the following commands<\/p>\n<pre>\nmkdir -p code4reference\/com\/jar\ntouch code4reference\/com\/jar\/TestJar.java\ntouch MainJar.java\n<\/pre>\n<p>The above commands create following directory structure.<\/p>\n<pre>\n.\n\u251c\u2500\u2500 code4reference\n\u2502   \u2514\u2500\u2500 com\n\u2502       \u2514\u2500\u2500 jar\n\u2502           \u2514\u2500\u2500 TestJar.java\n\u2514\u2500\u2500 MainJar.java\n<\/pre>\n<p>Now you can put some code in TestJar.java file. I have put something like this.<\/p>\n<pre>\npackage code4reference.com.jar;\n\npublic class  TestJar {\n\n   public void print(){\n      System.out.println(\"Hello World! from TestJar\");\n   }\n        \n}\n<\/pre>\n<p>As you may have noticed that the directory structure is resembling with the package name mentioned in the source file. Generally whenever you are making new java package you have to follow same directory structure. Otherwise some of the program would not be able to figure out your class file. Now it\u2019s time to compile the source code. It is necessary to compile the java source files before creating the jar file. Run the following command to compile the source file.<\/p>\n<pre>javac -sourcepath . code4reference\/com\/jar\/TestJar.java <\/pre>\n<p>In the above command <i>-sourcepath<\/i> is optional because the source file is present in the child directory. In-case source files are present in different directory then you need to provide the <i>-sourcepath<\/i> option. Javac command treat that value as the base directory for the source code.<br \/>\nOnce the above command is executed a class file will get generated in the respective directory. In this case s TestJar.class file gets generated in code4reference\/com\/jar directory. Now it\u2019s time to create jar file. Run the command given below.<\/p>\n<pre>jar -cf jartest.jar code4reference\/ <\/pre>\n<p>This command will create testjar.jar file in the current directory. You can refer the class(es) present in the jar file in java source file like the way it has been used in the source code below.<\/p>\n<pre>\nimport code4reference.com.jar.TestJar;\n\nclass MainJar{\n   public static void main(String args[]){\n      TestJar testjar = new TestJar();\n      testjar.print();\n  } \n}\n<\/pre>\n<p>Compile the source code using this command.<\/p>\n<pre>javac -cp testjar.jar MainJar.java <\/pre>\n<p>Once this command is executed you will see MainJar.class file in the current directory. Here <i>-cp<\/i> option has been used to give the referenced jar file name and location. Otherwise java compiler will throw class definition not found error. Now execute the command given below.<\/p>\n<pre>java -cp testjar.jar:.  MainJar\nHello World! from TestJar\n<\/pre>\n<p>Noticed that I have included \u201c.\u201d after <i>test.jar:<\/i> this gives a hint to the java application to look for the class in the jarfile as well as in the current directory. If this \u201c.\u201d is not provided then <i>java<\/i> throws this exception.<\/p>\n<pre>Error: Could not find or load main class MainJar <\/pre>\n<p>Please feel free to leave comments about post or website.Thanks <img alt='icon_wink-3338700' src='https:\/\/code4reference.com\/wp-content\/uploads\/2013\/06\/icon_wink-3338700.gif' \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a simple tutorial about making jar file and compiling the source code using command. As you may know that in java the class are packaged in jar file and provides as library. So it can be used in different module\/project. First we are going to make some suitable directory structure. Run the following [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/71","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=71"}],"version-history":[{"count":1,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":354,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/71\/revisions\/354"}],"wp:attachment":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}