This is one of my first java codes. Pardon me if the question is too naive.
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:
update.java:
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();
}
}
I compiled it using the following command:
javac -cp vendorcatalogapi.jar update.java
I am trying to run it using the following command:
java -cp vendorcatalogapi.jar -cp . MyFirstClass
But I am getting the following error:
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
How can I correctly set the classpath while running the Java program?
java -cp .;vendorcatalogapi.jar MyFirstClasswith windows,java -cp .:vendorcatalogapi.jar MyFirstClasswith Unix(-like). Just a hint: Give the.javafiles the same name of your java class. It will be much easier for you to find classes.