title: How do I compile my main Java program on the command line when it includes an external java package?
body: <p>I'm a relative Java newbie so apologies if the question appears somewhat basic. I've googled high and low for an answer here and I'm not finding anything that's helping. </p>
  
  <p>Problem: </p>
  
  <p>Whilst I'm able to integrate external packages into my Java programs from an IDE environment, I am trying to do run a very basic program from the command line that calls on a separate, basic package file that I have written - and am simply doing all this as I want to have a bottom-up understanding of how package files are related to a main program by Java.</p>
  
  <p>I have a program that sits on my desktop named MyProgram.java: </p>
  
  <pre><code>import org.somepackage;
  
  public class MyProgram {
  
      public static void main(String arguments[]) {
  
      System.out.println("Programme up and running...");
      Human myHuman = new Human(); 
      myHuman.scream();
  
    }
  </code></pre>
  
  <p>Still on the Desktop, I then have another folder which I've named <em>src</em>, inside of which I have created the necessary subfolders corresponding to the package name, i.e. <em>./src/org/somepackage</em> - and in this location, I have the Human.java file which defines the Human class with the following contents:</p>
  
  <pre><code>package org.somepackage;
  
  public class Human {
  
      public void scream() {
  
      System.out.println("I exist!!");
  
    }
  
  }
  </code></pre>
  
  <p>I then created a classes folder, again on the Desktop, and ran the following compile command on the command line: </p>
  
  <pre><code>javac -d ./classes/ ./src/org/packagename/Human.java
  </code></pre>
  
  <p>This ran fine and created - as expected - the Human.class file within the ./classes/org/packagename/ location. </p>
  
  <p>However, where I fall down is when I then try to compile MyProgram.java on the command line, i.e. </p>
  
  <pre><code>javac -cp ".:./classes/" MyProgram.java
  </code></pre>
  
  <p>As you'll see, my class path contains a reference to the current location (<em>"."</em>) for the MyProgram.java file, and it contains a reference to the classes folder (<em>"./classes/"</em>) which is the base location for the org.somepackage package inside whose subfolders (<em>./classes/org/somepackage/</em>) on can find the <em>Human.class</em> file. </p>
  
  <p>At this stage, I was simply expecting the java engine to compile <em>MyProgram.java</em> into the program MyProgram.class - but, instead, I get an error:</p>
  
  <p><em>MyProgram.java:1: error: package org does not exist</em></p>
  
  <p>I've been following the instructions listed here:</p>
  
  <p><a href="https://www3.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html" rel="nofollow noreferrer">https://www3.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html</a> </p>
  
  <p>and I don't appear to be deviating from the instructions - yet I'm unable to locate an explanation on Stackoverflow or anywhere else as to a  possible reason for this compile failure. If anyone has an idea, your help would be very much appreciated. </p>
  
  <p>Thanks, </p>