nish
nish

Reputation: 7280

Loading a class from an external jar

This is my first java program, so please excuse me if its too naive.

I have a 3rd party jar. I want to instantiate a class in the jar and be able to use its methods. Some details about the class in the jar:

Class File: rediff.inecom.catalog.product.CSVAPI
Constructor: CSVAPI()
Method: UpdateCSVAPI(key, csvpath)
Return: String

I have written the following program:

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.io.IOException;

class MyLoaderClass{
    public void myLoaderFunction(){
        File file  = new File("vendorcatalogapi.jar");
        try {
            URL url = file.toURI().toURL();  
            URL[] urls = new URL[]{url};
            ClassLoader cl = new URLClassLoader(urls);

            Class cls = cl.loadClass("rediff.inecom.catalog.product.CSVAPI");
            Object cls_object = cls.newInstance();
            System.out.println(cls_object);
            String output = cls_object.UpdateCSVAPI(12345,"myfile.csv");
            System.out.println(output);
            System.out.println("try");
        }
        catch (Exception e) { 
            System.out.println("catch");
            e.printStackTrace(); 
        }
    }

    public static void main(String args[]){
        new MyLoaderClass().myLoaderFunction();
    }
}

I am trying to compile it using:

javac -cp vendorcatalogapi.jar temp.java

But I am getting the following error:

temp.java:17: error: cannot find symbol
            String output = cls_object.UpdateCSVAPI(12345,"myfile.csv");
                                      ^
  symbol:   method UpdateCSVAPI(int,String)
  location: variable cls_object of type Object
1 error

Looks like the object is not correctly initialized. Please can someone help me with the correct way of doing it

Upvotes: 1

Views: 107

Answers (3)

Klas Lindbäck
Klas Lindbäck

Reputation: 33273

If this is your first java program, then loading the class dynamically is probably overkill. Just use it normally and let the default class loader load it:

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.io.IOException;
import rediff.inecom.catalog.product.CSVAPI;

class MyFirstClass{
    public void myFunction() {
        CSVAPI cvsapi = new CSVAPI();
        System.out.println(cvsapi);
        String output = cvsapi.UpdateCSVAPI(12345,"myfile.csv");
        System.out.println(output);

        System.out.println("Success!");
    }

    public static void main(String args[]){
        new MyFirstClass().myFunction();
    }
}

Compile (note that the source code file name must match the class name):

javac -cp vendorcatalogapi.jar MyFirstClass.java

Run:

java -cp .:vendorcatalogapi.jar MyFirstClass (on Unix based)
java -cp .;vendorcatalogapi.jar MyFirstClass (on Windows)

Upvotes: 2

Vinay Veluri
Vinay Veluri

Reputation: 6855

Object class doesnt know the methods of rediff.inecom.catalog.product.CSVAPI class.

        Class cls = cl.loadClass("rediff.inecom.catalog.product.CSVAPI");
        Object cls_object = cls.newInstance();

So, explicit casting is required

rediff.inecom.catalog.product.CSVAPI object = 
                  (rediff.inecom.catalog.product.CSVAPI) cls.newInstance();

will do the job.

Upvotes: 1

luanjot
luanjot

Reputation: 1176

You have to let the compiler know that cls_object is an instance of CSVAPI. If you don't, you can only use the object methods (toString, equals, etc.).

To do this, you can do the following:

 rediff.inecom.catalog.product.CSVAPI cls_object = (rediff.inecom.catalog.product.CSVAPI) cls.newInstance();

Please, note that you need to have CSVAPI in your classpath!

Upvotes: 2

Related Questions