title: Running Java program with classpaths
body: <p>This is one of my first java codes. Pardon me if the question is too naive.</p>
  
  <p>I have an external jar file, an API. I want to be able to call the methods in this API using a java program. I wrote the following code:</p>
  
  <p><code>update.java</code>:</p>
  
  <pre><code>import java.io.File;
  import java.net.URL;
  import java.net.URLClassLoader;
  import java.io.IOException;
  import rediff.inecom.catalog.product.CSVAPI;
  
  class MyFirstClass{
      private final static String api_key = "xyz";
      private final static String path = "/path/to/myfile.csv";
  
      public void myFunction() {
          CSVAPI cvsapi = new CSVAPI();
          System.out.println(cvsapi);
          try {
              String output = cvsapi.UpdateCSVAPI(api_key,path);
              System.out.println(output);
              System.out.println("Success!");
          }
          catch (Exception e) {
              System.out.println("catch");
              e.printStackTrace(); 
          }
      }
  
      public static void main(String args[]){
          new MyFirstClass().myFunction();
      }
  }
  </code></pre>
  
  <p>I compiled it using the following command:</p>
  
  <pre><code>javac -cp vendorcatalogapi.jar update.java
  </code></pre>
  
  <p>I am trying to run it using the following command:</p>
  
  <pre><code>java -cp vendorcatalogapi.jar -cp . MyFirstClass
  </code></pre>
  
  <p>But I am getting the following error:</p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: rediff/inecom/catalog/product/CSVAPI
      at MyFirstClass.myFunction(update.java:12)
      at MyFirstClass.main(update.java:26)
  Caused by: java.lang.ClassNotFoundException: rediff.inecom.catalog.product.CSVAPI
      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:425)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
      ... 2 more
  </code></pre>
  
  <p>How can I correctly set the classpath while running the Java program?</p>