{"id":11377,"date":"2015-07-13T06:39:52","date_gmt":"2015-07-13T06:39:52","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=11377"},"modified":"2015-07-13T06:39:52","modified_gmt":"2015-07-13T06:39:52","slug":"java-running-a-class-from-a-package","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/java-running-a-class-from-a-package\/","title":{"rendered":"Java:  running a class from a package"},"content":{"rendered":"<h1>The tutor discusses a trick of command-line Java.<\/h1>\n<p>Programmers are trained to organize related files in coordinated folders.  Older people &#8211; or Linux people &#8211; might prefer &#8220;directory&#8221; to &#8220;folder.&#8221;<\/p>\n<p>However you see it, a way to organize related files in Java is to identify them as belonging to the same package.  All the members of that package are stored in the same directory, which is named the same as the package.<\/p>\n<p>Each file in a package must declare, on its first line, <\/p>\n<p><span style=\"font-family:monospace\">package <i>nameofpackage<\/i>;<\/span><\/p>\n<p>Let&#8217;s imagine, for instance, a package called P1.  Then each file in it contains the first line<\/p>\n<p><span style=\"font-family:monospace\">package <i>P1<\/i>;<\/span><\/p>\n<p>Every file in that package is stored in the folder <span style=\"font-family:monospace\">P1<\/span><\/p>\n<p>A file in a package can be compiled from within the package directory, but running it might be easier to do from the directory above it.  Let&#8217;s image that, in the package P1, the file containing the <span style=\"font-family:monospace\">main()<\/span> method is called P1Driver.  Furthermore, let&#8217;s imagine the full path to P1Driver is<\/p>\n<p>\/javaprojects\/P1\/P1Driver.java  <span style=\"color:orange\">&nbsp;&nbsp;&nbsp;(in Linux)<\/span><\/p>\n<p>or<\/p>\n<p>c:\/javaprojects\/P1\/P1Driver.java <span style=\"color:orange\">&nbsp;&nbsp;&nbsp;(Windows)<\/span><\/p>\n<p>In either case, one way to run P1Driver, <u>once compiled<\/u>, might be to back out to the directory above it:<\/p>\n<p>\/javaprojects or c:\/javaprojects <\/p>\n<p>then, from there, try the command<\/p>\n<p><span style=\"font-family:monospace\">java P1.P1Driver<\/span> &nbsp;&nbsp;&nbsp;<span style=\"color:orange\">(Linux)<\/span><\/span><\/p>\n<p>or, in Windows:<\/p>\n<p><span style=\"font-family:monospace\">java -cp . P1.P1Driver<\/span><\/p>\n<p>There are many fine points to discuss concerning Java.  HTH:)<\/p>\n<p>Source:<\/p>\n<p>Schildt, Herbert.  <u>Java:  the Complete Reference<\/u>.  New York:  McGraw Hill, 2011.<\/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>The tutor discusses a trick of command-line Java. Programmers are trained to organize related files in coordinated folders. Older people &#8211; or Linux people &#8211; might prefer &#8220;directory&#8221; to &#8220;folder.&#8221; However you see it, a way to organize related files &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/java-running-a-class-from-a-package\/\"> <span class=\"screen-reader-text\">Java:  running a class from a package<\/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":[761,927,929],"class_list":["post-11377","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-java","tag-packages","tag-running-a-file-from-a-java-package"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/11377","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=11377"}],"version-history":[{"count":24,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/11377\/revisions"}],"predecessor-version":[{"id":11401,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/11377\/revisions\/11401"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=11377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=11377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=11377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}