title: java classpath mystery: either my classes or a jar, but not both?
body: <p>Consider the following java program:</p>
  
  <pre><code>import computer_package.Computer;
  import com.sun.jna.Library;
  public class HelloWorld {
      public static void main(String[] args) {
          Computer computer = new Computer();
          System.out.println("Hello, World");
      }
  }
  </code></pre>
  
  <p>and the following directory structure:</p>
  
  <pre><code>Rebs-MacBook-Pro:jalint2 rebcabin$ find computer_package/
  computer_package/
  computer_package//Computer.class
  computer_package//Computer.java
  </code></pre>
  
  <p>wherein <code>Computer.java</code> is as follows:</p>
  
  <pre><code>package computer_package;
  public class Computer {
      public Computer() {
          System.out.println("Constructor of Computer class.");
      }
      public void computer_method() {
          System.out.println("Power gone! Shut down your PC soon...");
      }
  }
  </code></pre>
  
  <p>and another directory with just <code>jar</code> files:</p>
  
  <pre><code>Rebs-MacBook-Pro:jalint2 rebcabin$ find com
  com
  com/sun
  com/sun/jna
  com/sun/jna/jna-4.0.0.jar
  com/sun/jna/jna-platform-4.0.0.jar
  </code></pre>
  
  <p>If I comment out just the <code>import com.sun.jna;</code> line in the original <code>HelloWorld.java</code> file, then all works well </p>
  
  <pre><code>Rebs-MacBook-Pro:jalint2 rebcabin$ javac -g HelloWorld.java 
  Rebs-MacBook-Pro:jalint2 rebcabin$ java HelloWorld
  Constructor of Computer class.
  Hello, World
  </code></pre>
  
  <p>Javac and java are able to find the class file <code>./computer_package/Computer.class</code> without any further hints or annotation or classpath-hacking.</p>
  
  <p>Now, if I restore the <code>import com.sun.jna;</code> line and comment out just the <code>import computer_package.Computer;</code> line and the line the constructs a <code>new Computer</code>, and compile like this:</p>
  
  <pre><code>Rebs-MacBook-Pro:jalint2 rebcabin$ javac -g -cp "./com/sun/jna/jna-4.0.0.jar" HelloWorld.java
  </code></pre>
  
  <p>or even like this:</p>
  
  <pre><code>Rebs-MacBook-Pro:jalint2 rebcabin$ make
  javac -g -cp "./com/sun/jna/jna-4.0.0.jar:./computer_package" HelloWorld.java
  </code></pre>
  
  <p>then all is still well:</p>
  
  <pre><code>Rebs-MacBook-Pro:jalint2 rebcabin$ java HelloWorld
  Hello, World
  </code></pre>
  
  <p>At least, the <code>import com.sun.jna;</code> line didn't throw a compiler error -- admittedly, I am not <em>yet</em> using the jna library; I'm just trying to build up to that momentous achievement in baby steps.</p>
  
  <p>So if I now cautiously uncomment the <code>import computer_package;</code> line and the line that constructs an instance of <code>Computer</code>, yielding the <code>HelloWorld.java</code> file at the top of this question that tries to use <em>both</em> my <code>Computer</code> class <em>and</em> jna, all hell breaks loose; the previously acceptable <code>Computer</code> class is now inaccessible:</p>
  
  <pre><code>Rebs-MacBook-Pro:jalint2 rebcabin$ make
  javac -g -cp "./com/sun/jna/jna-4.0.0.jar:./computer_package" HelloWorld.java
  HelloWorld.java:20: error: package computer_package does not exist
  import computer_package.Computer;
                         ^
  HelloWorld.java:25: error: cannot access Computer
          Computer computer = new Computer();
          ^
    bad class file: ./computer_package/Computer.class
      class file contains wrong class: computer_package.Computer
      Please remove or make sure it appears in the correct subdirectory of the classpath.
  2 errors
  make: *** [HelloWorld.class] Error 1
  </code></pre>
  
  <p>I know this is elementary; I am not a java expert and I admit I am trying to guess my way around the classpath nightmare world; I am using just emacs and bash, avoiding the IDEs that add even more layers of opaque abstraction. </p>