{"id":32,"date":"2010-03-10T01:45:02","date_gmt":"2010-03-10T04:45:02","guid":{"rendered":"http:\/\/www.samontab.com\/web\/?p=32"},"modified":"2010-03-10T01:45:02","modified_gmt":"2010-03-10T04:45:02","slug":"compiling-java-code","status":"publish","type":"post","link":"https:\/\/www.samontab.com\/web\/2010\/03\/compiling-java-code\/","title":{"rendered":"Compiling Java Code"},"content":{"rendered":"<p>Java is a very popular language. Here is an introduction on how to compile and run Java programs. The first step is to download and install the latest Java Development Kit (JDK), which allows you to compile and run java code. Note that there is a Java Runtime Environment (JRE) which only allows you to run applications made in Java, you have to download the JDK which contains the development and runtime environments. You can download it from <a title=\"Java SDK Download\" href=\"http:\/\/java.sun.com\/javase\/downloads\" target=\"_blank\">here<\/a>.<\/p>\n<p>The next step is to write a simple Java program. Using any plain text editor, write the following code:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass mainClass\r\n{\r\n    public static void main(String args&#x5B;])\r\n    {\r\n    System.out.println(&quot;Hello there...&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p>The filename has to be the same as the name of the class defined in the code. In this case, the class that I created is called <em>mainClass<\/em>. Also, the filename needs to have a .java extension. Therefore, the file has to be saved under the name of <strong>mainClass.java<\/strong>. Now you can compile the code using javac which is the Java compiler.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\njavac *.java\r\n<\/pre>\n<p>If you need to include some other folders with pre-compiled .class files or other libraries in .jar format, you need to use the <strong>-classpath <\/strong>argument. Note that for each .jar you need to specify the complete path. Each inclusion is separated with a semicolon (<strong>;<\/strong>). Remember to include the current directory by adding a single dot (<strong>.<\/strong>).<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\njavac -classpath .;&quot;c:\\path\\to\\classFiles&quot;;&quot;c:\\path\\to\\lib1.jar&quot; *.java\r\n<\/pre>\n<p>Now the code is ready to be run. Just call java and the name of the class that contains the Main function, which in this case is <em>mainClass<\/em>.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\njava mainClass\r\n<\/pre>\n<p>You can also pack your application into a Jar file, which basically is a compressed file containing the machine code you already compiled and a manifest file. The manifest file tells extra information about the application, in  particular which one is the main class to execute. Let&#8217;s create this file. Open a text editor and write the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\r\nMain-Class: mainClass\r\nManifest-Version: 1.0\r\n\r\n<\/pre>\n<p>As you can see, you have to define which is the Main-Class<em><\/em> that is going to be executed when the Jar file is run. Save this file as <strong>Manifest.txt<\/strong>. Although the second line itself is optional, it is important to add it (or even just a blank line) below the Main-Class definition because the Main-Class line needs to contain an end of line character in order to work properly. If you do not add an end of line character at the end of the Main-Class line you will receive the following error when you try to run your Jar file:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nFailed to load Main-Class manifest attribute from main.jar\r\n<\/pre>\n<p>Jar is the application that allows you to pack your application. The c argument means that you are creating a Jar file. The f argument allows you to give a filename instead of sending the output to the console. The m argument means that you are telling the program where the  manifest file is. Then, you need to provide the jar filename and the manifest filename. In this case, I used the <strong>main.jar<\/strong> filename but you can use any  name as long as it has a .jar extension. The manifest filename can be anything you want, in this case I used <strong>Manifest.txt<\/strong> which was created previously. The last argument tells the program which files to include in the Jar package, in this case I simply used all the files that end in .class which is general enough for this introduction.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\njar cfm main.jar Manifest.txt *.class\r\n\r\n<\/pre>\n<p>Now you have your application packed in a Jar file. If you want to run it, you only need to write this:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\njava -jar main.jar\r\n<\/pre>\n<p>Now you are able to compile and run Java programs.<\/p>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;\">\n<pre><span style=\"color: blue;\">class<\/span> myfirstjavaprog\r\n{\r\n        public static void main(String args[])\r\n        {\r\n           System.out.println(\"Hello World!\");\r\n        }\r\n}\r\n<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Java is a very popular language. Here is an introduction on how to compile and run Java programs. The first step is to download and install the latest Java Development Kit (JDK), which allows you to compile and run java code. Note that there is a Java Runtime Environment (JRE) which only allows you to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":0,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}