{"id":10882,"date":"2015-06-07T19:31:32","date_gmt":"2015-06-07T19:31:32","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=10882"},"modified":"2015-06-07T19:31:32","modified_gmt":"2015-06-07T19:31:32","slug":"java-programming-compiling-and-running","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/java-programming-compiling-and-running\/","title":{"rendered":"Java programming:  compiling and running"},"content":{"rendered":"<h1>Following up yesterday&#8217;s post, the tutor offers basic instructions to compile and run a Java program.<\/h1>\n<p>I&#8217;ve never used a Java IDE; maybe I&#8217;ll try a few and write future posts about them.  For now, my readers know that I use the command line.<\/p>\n<p>Let&#8217;s imagine you&#8217;ve written a standalone Java class such as<br \/>\n<span style=\"font-family:monospace\"><br \/>\nclass P0{<br \/>\npublic static void main(String[] args){<br \/>\nSystem.out.println(&#8220;Congratulations from Java!&#8221;);<br \/>\n}<br \/>\n}<\/p>\n<p>To succeed, the file needs to be saved with the <span style=\"font-family:monospace\">.java<\/span> file extension.  Let&#8217;s imagine it&#8217;s saved <span style=\"font-family:monospace\">P0.java<\/span> in the folder <span style=\"font-family:monospace\">java_demos<\/span>.  To compile it, you&#8217;d open the terminal, then enter that directory.  Next, you&#8217;d key in<\/p>\n<p><span style=\"font-family:monospace\">javac P0.java<\/span><\/p>\n<p>If you get error messages, you&#8217;ll need to do some proofing.  If the program compiles fine, you likely won&#8217;t get a message at all; rather, you&#8217;ll just see the command prompt appear on the next line.<\/p>\n<p>Running the program can depend on your operating system.<\/p>\n<p>From the Linux terminal, you can run the program thus:<\/p>\n<p><span style=\"font-family:monospace\">java P0<\/span><\/p>\n<p>However, from the Windows terminal, you might need to key in<\/p>\n<p><span style=\"font-family:monospace\">java -cp . P0<\/span><\/p>\n<p>In the case of <a href=\"?p=10864\">yesterday&#8217;s post<\/a>, there is a class that uses another.  In that case, the compile command should indicate both files (the one containing the <span style=\"font-family:monospace\">main() <\/span> method, listed first):<\/p>\n<p><span style=\"font-family:monospace\">javac MessagerDriver.java Messager.java<\/span><\/p>\n<p>However, after a successful compile, the program can be run by calling only the first file:<\/p>\n<p>(Linux)<\/p>\n<p><span style=\"font-family:monospace\">java MessagerDriver<\/span><\/p>\n<p>(Windows)<\/p>\n<p><span style=\"font-family:monospace\">java -cp . MessagerDriver<\/span><\/p>\n<p>Programs rarely compile and run the first try; usually, corrections need to be made because of missing parentheses, semicolons, or what have you.  The compiler tries to tell what&#8217;s wrong, but in my experience, a missing bracket can lead to ten error messages as it tries to make sense of the code without it.  Very often, one or two simple problems are all that&#8217;s wrong.<\/p>\n<p>Good luck with your coding.  I have so much to follow up from this post:)<\/p>\n<p>Source:<\/p>\n<p>Niemeyer, Patrick and Jonathan Knudsen.  <u>Learning Java<\/u>.  Sebastopol, CA:  O&#8217;Reilly &nbsp;&nbsp;Media, Inc, 2005.<\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following up yesterday&#8217;s post, the tutor offers basic instructions to compile and run a Java program. I&#8217;ve never used a Java IDE; maybe I&#8217;ll try a few and write future posts about them. For now, my readers know that I &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/java-programming-compiling-and-running\/\"> <span class=\"screen-reader-text\">Java programming:  compiling and running<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[865,867,863,602,862,866,864],"class_list":["post-10882","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-compiling-a-java-class","tag-compiling-a-java-class-that-depends-on-another-class","tag-compiling-a-java-program","tag-java-programming","tag-javac","tag-running-a-java-class","tag-running-a-java-program"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/10882","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=10882"}],"version-history":[{"count":24,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/10882\/revisions"}],"predecessor-version":[{"id":10906,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/10882\/revisions\/10906"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=10882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=10882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=10882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}