title: NoClassDefFoundError: com/google/common/base/Preconditions, but Preconditions is in the .jar file in my runtime classpath
body: <p>I have a project that uses <code>ImmutableList</code> and <code>ImmutableSet</code> from Guava. It compiles just fine, but at runtime, with the same <code>-cp</code>, it gives this error message:</p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Preconditions
      at com.google.common.collect.RegularImmutableList.get(RegularImmutableList.java:81)
      at BasicPoint.pointAsArray(BasicPoint.java:93)
      at BasicPoint.inflate(BasicPoint.java:134)
      at BasicEdgeLength.&lt;clinit&gt;(BasicEdgeLength.java:32)
  Caused by: java.lang.ClassNotFoundException: com.google.common.base.Preconditions
      at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      ... 4 more
  </code></pre>
  
  <p>I compile it in the terminal with this command</p>
  
  <pre><code>javac -cp '.:/MYPROJECTDIRECTORY/guava-14.0.1.jar'  BasicEdgeLength.java
  </code></pre>
  
  <p>and it compiles. Then I run it with this command</p>
  
  <pre><code>java -cp '.:/MYPROJECTDIRECTORY/guava-14.0.1.jar'  BasicEdgeLength
  </code></pre>
  
  <p>and I get the aforementioned error message. There are <a href="https://stackoverflow.com/questions/10512739/how-to-solve-noclassdeffounderror-preconditions">other</a> <a href="https://stackoverflow.com/questions/7457668/google-api-exception-in-thread-main-java-lang-noclassdeffounderror-com-googl">questions</a> on stackoverflow about the exact same issue, but the answer is always to download the guava libraries, which I have already done. Indeed, when I poke around in <a href="https://code.google.com/p/guava-libraries/" rel="nofollow noreferrer">guava-14.0.1.jar</a>, I can see the file <code>com/google/common/base/Preconditions</code>. The fact that I can see in the .jar file the very class mentioned in the error message makes me imagine that I can't solve this problem just by downloading more files. So I'm not sure what to do, and any suggestions that you could offer would be greatly appreciated.</p>
  
  <p>I'm including the code from <code>BasicPoint</code> below, because that's the class that calls <code>RegularImmutableList.get(i)</code>, although please be aware that BasicPoint won't compile without <code>AbstractPoint</code>, <code>IntMatrix</code>, <code>Initializer</code>, and possibly some other stuff (and it doesn't contain the main method, which is found in <code>BasicEdgeLength</code>). I'm happy to append the code to some of these other classes if anyone thinks it's necessary.</p>
  
  <p>Thank you all for your time!</p>
  
  <pre><code>/**
  *    This class implements a point.
  *    It uses a representation as a vector of integers.  
  */
  
  import com.google.common.collect.*;
  //import com.google.common.base.*; // same error occurs whether or not I include this line
  
  final public class BasicPoint implements AbstractPoint&lt;BasicPoint, BasicAngle&gt; {
  
  // static variables for all points.
  private static final int length = Initializer.N - 1;
  
  private static final IntMatrix ROT = Initializer.ROT;
  
  private static final IntMatrix REF = Initializer.REF;
  
  private static final IntMatrix INFL = Initializer.INFL;
  
  // A vector identifying the point.  
  private final ImmutableList&lt;Integer&gt; point;
  
  // Constructor methods.
  
  private BasicPoint(Integer[] vector) {
      if (vector.length != length) {
          throw new IllegalArgumentException("Point length is incorrect.");
      }
      this.point = ImmutableList.copyOf(vector);
  }
  
  private BasicPoint() {
      Integer[] vector = new Integer[length];
      for (int i = 0; i &lt; length; i++) {
          vector[i] = 0;
      }
      point = ImmutableList.copyOf(vector);
  }
  
  // public static factory method
  static public BasicPoint createBasicPoint(Integer[] vector) {
      return new BasicPoint(vector);
  }
  
  static final public BasicPoint zeroVector() {
      return new BasicPoint();
  }
  
  static final public BasicPoint unitVector() {
      Integer[] vector = new Integer[length];
      vector[0] = 1;
      for (int i = 1; i &lt; length; i++) {
          vector[i] = 0;
      }
      return new BasicPoint(vector);
  }
  
  // toString method.
  public String toString() {
      String outString = "(";
      for (int i = 0; i &lt; point.size() - 1; i++) {
          outString = outString + point.get(i) + ",";
      }
      outString = outString + point.get(length) + ")";
      return outString;
  }
  
  // equals method.
  public boolean equals(Object obj) {
      if (obj == null || getClass() != obj.getClass())
          return false;
      BasicPoint p = (BasicPoint) obj;
      for (int i = 0; i &lt; length; i++) {
          if (p.point.get(i) != this.point.get(i))
              return false;
      }
      return true;
  }
  
  // hashCode override.
  public int hashCode() {
      int prime = 53;
      int result = 11;
      for (int i = 0; i &lt; length; i++) {
          result = prime*result + point.get(i);
      }
      return result;
  }
  
  // a private helper method to turn point into an array of Integers.
  private Integer[] pointAsArray() {
      Integer[] output = new Integer[length];
      for (int i = 0; i &lt; length; i++)
          output[i] = point.get(i);
      return output;
  }
  
  // Manipulation methods.  
  public BasicPoint add(BasicPoint p) {
      Integer[] q = new Integer[length];
      for (int i = 0; i &lt; length; i++) {
          q[i] = this.point.get(i) + p.point.get(i);
      }
      return new BasicPoint(q);
  }
  
  public BasicPoint scalarMultiple(int c) {
      Integer[] q = new Integer[length];
      for (int i = 0; i &lt; length; i++) {
          q[i] = c * this.point.get(i);
      }
      return new BasicPoint(q);
  }
  
  public BasicPoint subtract(BasicPoint p) {
      return this.add(p.scalarMultiple(-1));
  }
  
  public BasicPoint rotate(BasicAngle a) {
      int i = a.getAsInt();
      if (i &lt; 0)
          throw new IllegalArgumentException("You must perform a positive number of rotations.");
  
      Integer[] result  = new Integer[length];
      for (int j = 0; j &lt; i; j++)
          result = ROT.rowTimes(result);
      return new BasicPoint(result);
  }
  
  public BasicPoint reflect() {
      return new BasicPoint(REF.rowTimes(this.pointAsArray()));
  }
  
  public BasicPoint inflate() {
      return new BasicPoint(INFL.rowTimes(this.pointAsArray()));
  }
  
  } // end of class BasicPoint
  </code></pre>