1

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?

2
  • 1
    Use java -cp .;vendorcatalogapi.jar MyFirstClass with windows, java -cp .:vendorcatalogapi.jar MyFirstClass with Unix(-like). Just a hint: Give the .java files the same name of your java class. It will be much easier for you to find classes. Commented Sep 16, 2014 at 9:00
  • Bdw have u set classpath in environment varaibles?? Commented Sep 16, 2014 at 9:01

2 Answers 2

4

try executing this way in windows

java -cp .;path/to/vendorcatalogapi.jar MainClass

in linux environment

java -cp .:path/to/vendorcatalogapi.jar MainClass
Sign up to request clarification or add additional context in comments.

Comments

3

The java and javac commands accept only one cp or classpath parameter.

If you have multiple classpath elements, you can concatenate them using a separator element which is ';' on windows and ':' on unix systems:

javac -cp .;vendorcatalogapi.jar update.java
java -cp .;vendorcatalogapi.jar MyFirstClass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.